Building a SQL parameter query - LIKE% in .cs and grid view

I am trying to implement admin and operator login (india, australia, america), now the operator id starts with "IND123" "AUS123" and "AM123"

when the india operator registers, it can only see the details of members with id 'IND%', the same applies to Australian and American users

and when admin logs in, he can see details of members with id 'IND%' or 'AUS%' or 'AM%'

I have a table that defines the role ie admin or operator and their prefix (IND, AUS respectively)

On the login page, I created a session for the role and the prefix PREFIX = myReader ["Prefix"]. ToString (); Session ["LoginRole"] = myReader ["Role"]. ToString (); Session ["LoginPrefix"] = String.Concat (PREFIX + "%"); works great

On the main page (after login) I need to count the number of members, so I wrote

    string prefix = Session["LoginPrefix"].ToString();
    string role = Session["LoginRole"].ToString();

    if (role.Equals("ADMIN"))
            StrMemberId = "select count(*) from MemberList";
    else
        StrMemberId = "select count(*) from MemberList where MemberID like '"+prefix+"'";

      

thatworks is great too

Problem: 1. I want the constructor StrMemberId = "select count (*) from MemberList where MemberID is like '@prefix +'";

myCommd.Parameters.AddWithValue (@ prefix, prefix); What does not work

2 Displaying items in a gridview I need to give a condition (something like if (role.Equals ("ADMIN")) shows all the members that show the member depending on the operator prefix) a list of members in operator and admin mode - where to put the condition in gridview how to apply these

suggest anything Relationship

Indranil

+2


a source to share


2 answers


You need to build your query like this:

"select count(*) from MemberList where MemberID like @prefix"

      



then

cmd.Parameters.AddWithValue("@prefix", prefix + "%")

+2


a source


  • It should be "select count(*) from MemberList where MemberID like @prefix";

  • You can do all of these checks in a subroutine and return the results accordingly. The result can then be linked to the gridview


+1


a source







All Articles