Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 22-Jan-13 1:12am
Edited 22-Jan-13 1:59am
digimanus26.6K
v3
Comments
Nandakishorerao at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Member 9378829 at 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)

  Print Answers RSS
0 OriginalGriff 370
1 Sergey Alexandrovich Kryukov 195
2 Prakriti Goyal 177
3 jlopez788 134
4 _Amy 110
0 OriginalGriff 6,762
1 Sergey Alexandrovich Kryukov 5,539
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100