0

// aspx code

   <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="fullname" DataValueField="username" AutoPostBack="True" Width="125px" Height="22px"></asp:DropDownList>

// Code behind

 SqlConnection myConnection = new SqlConnection("Server=xyz;Database=db;Uid=db;Pwd=12345");
 myConnection.Open();         
 SqlDataReader myReaderddl = null;
 SqlCommand myCommandddl = new SqlCommand("SELECT [username], [fullname] FROM [qa_users]", myConnection);
 myReaderddl = myCommandddl.ExecuteReader();
 myReaderddl.Read();
 ddlrep.DataSource = myReaderddl;                    
 ddlrep.DataValueField = "[username]";
 ddlrep.DataTextField = "[fullname]";
 ddlrep.DataBind();
 myReaderddl.Close();
7
  • Where in code are you putting this? Page_Load? Can you please provide markup describing DDL as well as context of code-behind? Commented Jul 10, 2012 at 12:41
  • Where/how did you define ddlrep ? Commented Jul 10, 2012 at 12:42
  • More information required. I tidied up your code for you Commented Jul 10, 2012 at 12:44
  • Yes I have this code in Page_Load event Commented Jul 10, 2012 at 12:45
  • is data present in the table also check it Commented Jul 10, 2012 at 12:46

4 Answers 4

2

I haven't tested this code, but it should work.

using (var myConnection = new SqlConnection("Server=xyz;Database=db;Uid=db;Pwd=12345"))
{
    using (var myCommandddl = new SqlCommand("SELECT [username], [fullname] FROM [qa_users]", myConnection))
    {
        var table = new DataTable();

        using(var myAdapter = new SqlDataAdapter(myCommandddl))
            myAdapter.Fill(table);

        ddlrep.DataSource = table;
        ddlrep.DataValueField = "username";
        ddlrep.DataTextField = "fullname";
        ddlrep.DataBind();
    }
}

There's a couple changes I made that you should note:

Sign up to request clarification or add additional context in comments.

2 Comments

This worked. im not sure what issue was. but worked fine. Thanks
I think the issue was trying to assign a SqlDataReader to the DataSource property. The DataSource property expects an instance of IEnumerable, IListSource, IDataSource, or IHierarchicalDatasource.
0

Update your codebehind to look like the following:

if (!IsPostBack)
{

   SqlConnection myConnection = new SqlConnection("Server=xyz;Database=db;Uid=db;Pwd=12345");
    try 
    {
       myConnection.Open();         
       SqlDataReader myReaderddl = null;
       SqlCommand myCommandddl = new SqlCommand("SELECT [username], [fullname] FROM [qa_users]", myConnection);
       myReaderddl = myCommandddl.ExecuteReader();
       if (myReaderddl.HasRows)
       {
          ddlrep.DataSource = myReaderddl;                    
          ddlrep.DataValueField = "username";
          ddlrep.DataTextField = "fullname";
          ddlrep.DataBind();
       }
    }
    finally 
    {
       if (myConnection != null)
       {
          myConnection.Close();
       }
    }
}

7 Comments

When you break on myReaderddl = myCommanddl.ExecuteReader(), do you ever go into the if block? Is data being returned?
data is pulled but its not binding to the drop down list
Torres:: still not displaying
Just for giggles, instead of doing the binding, if you were to replace the binding with: while(myReaderddl.Read()) { ddlRep.Items.Add(new ListItem(myReaderddl["fullname"], myReaderddl["username"]));}, does it work? Does it iterate over anything?
either way its pulling data but not displaying.
|
0

Your DropDownlist Should be like this:

<asp:DropDownList ID="ddlrep" runat="server" Width="200px" ></asp:DropDownList>

and In code behind, in pageload or in any methode

SqlConnection myConnection = new SqlConnection("Server=xyz;Database=db;Uid=db;Pwd=12345");
 myConnection.Open();         
 SqlCommand myCommandddl = new SqlCommand("SELECT username, fullname FROM qa_users", myConnection);
 SqlDataReader myReaderddl = myCommandddl.ExecuteReader();
 if(myReaderddl.HasRows)
 {
 ddlrep.DataSource = myReaderddl;                    
 ddlrep.DataValueField = "username";
 ddlrep.DataTextField = "fullname";
 ddlrep.DataBind();
 }
 myReaderddl.Close();

in the pageload put the code under this block

if (!IsPostBack)
        {
        }

12 Comments

if possible then debug it..and see its coming inside the if block or not...use the modified query remove Brackets..
I debugged, data is there but its not binding.
ok do one thing remove the dropdown list and paste the code what i mentioned for drop down..the first one.
I tried your previous code too. One thing is for sure its pulling data but its not displaying
SqlDataReader myReaderddl = null;SqlCommand myCommandddl = new SqlCommand("SELECT username, fullname FROM qa_users", myConnection); myReaderddl = myCommandddl.ExecuteReader(); myReaderddl.Read(); if (myReaderddl.HasRows) { ddlrep.DataSource = myReaderddl; ddlrep.DataValueField = "username"; ddlrep.DataTextField = "fullname"; ddlrep.DataBind(); }myReaderddl.Close();
|
0

This is some of my old code so it is not pretty ( I use EF these days), but try somehting like this:

SqlCommand cmd = new SqlCommand("SELECT [username], [fullname] FROM [qa_users]", myConnection);

cmd.Connection.Open();
SqlDataReader data_table = null;
data_table = cmd.ExecuteReader();
ddlrep.DataSource = data_table;
ddlrep.DataValueField = "username";
ddlrep.DataTextField = "fullname";
ddlrep.DataBind();
data_table.Close();
cmd.Connection.Close();
cmd.Connection.Dispose();

I pulled this from a working project so it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.