Click here to Skip to main content
12,622,032 members (33,776 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: .NET3.5 C# .NET SQL-Server , +
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 2:12am
Updated 22-Jan-13 2:59am
v3
Comments
Nandakishorerao 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161128.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2016
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