Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
private void AutoradioButton1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Server=CHRIS-PC\\SQLEXPRESS;Database=jasmineSql.sdf;" +
                    "Integrated Security=True");
            conn.Open();
            DataSet sds = new DataSet();
            SqlCommand scmd = new SqlCommand("SELECT Description FROM TblCategories", conn);
                        SqlDataAdapter sadptr = new SqlDataAdapter(scmd);
                        sadptr.Fill(sds, "TblCategories");   
listBox1.DataSource = sds;
      listBox1.DataBind();  
            }

Populating a Listbox from the desired column in a Particular table (I have 3 table in a my database)

Please can you tell me what is going worng. I coudn't see the Values entered in the column "TblCategories" in my listBox1 when I clicked the radio button.


Note: I am getting ListBox1.Databindings(); Not listBox1.DataBind();
Is this causing to not populate. Please help.

Can anyone provide me with a solution?
Thanks.
Posted
Updated 19-Sep-10 1:14am
v6
Comments
Baji Jabbar 19-Sep-10 13:09pm    
At least a simple googling has been done , before posting here :)

1 solution

Hi,

assuming you're asking for a WinForms solution and your "listbox1" is a standard ListBox control, your problem is - apart from the last line, which prevents your code from getting compiled - the wrong DataSource.

A DataSet is a set of Tables, Rows, Relations ... so it is of no use as a DataSource. If you want to display a specific column from a specific table from your DataSet, you must specify this. So use
this.listBox1.DataSource = sds.Tables["TblCategories"];
this.listBox1.DisplayMember = "Description";

instead of
listBox1.DataSource = sds;

And maybe you should consider using "using"-blocks to secure the disposal of the SqlConnection, the SqlCommand and the SqlAdapter.

Cheers
Jürgen
 
Share this answer
 

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