Click here to Skip to main content
16,019,152 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
C#
private void load_uname()
    {
username.Items.Clear();

        SqlConnection con = new SqlConnection(constring);
        con.Open();
        string sql = "select [uname] from user_registration where usergroup ='" + usertype.SelectedValue.ToString() + "' ";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        username.Items.Add("<------Select---->");
        while (dr.Read())
        {

            username.Items.Add(new ListItem(dr[0].ToString()));
        }
    }

this is my code which is not working if i put my query statically like this "select [uname] from user_registration where usergroup ='owners'", it's working but if i tried dynamically like above query in the code not fetching the data!!
Posted
Updated 22-Jan-13 1:59am
v3
Comments
Nandakishore G N 22-Jan-13 7:22am    
send the selecteditem.tostring not value....

string sql = "select [uname] from user_registration where usergroup ='" + usertype.SelectedValue.ToString() + "' ";

if you are populating value with numbers and expecting to send the string..change it and verify it again...

try this
string sql = "select [uname] from user_registration where usergroup ='" + usertype.SelectedItem.ToString() + "' ";

1 solution

You are expecting a string value wheras SelecetedValue will fetch for the Value against the string data given.
Say your Owners has 2 persons XYZ with ID 100 and ABC with ID 200

CASE 1:Selected value will search in owners by 100 or 200.

CASE 2:Selected Item will search in owners by XYZ OR ABC.

Since your are searching through string but you have specified Selected Value, it is showing blank.

Try case 2, it will work
 
Share this answer
 
Comments
Member 9378829 23-Jan-13 0:33am    
Even case 2 also skipping the while loop

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900