How to Databind to ListBox
List of detail how to show listbox coding method
asp.net
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
deva nathan20 reputationPosted Nov 21, 2012, 10:56 PM EST
{
SqlConnection d = new SqlConnection("Data Source=pits12-PC;Initial Catalog=devaexample;Persist Security Info=True;User ID=sa;Password=test");
d.Open();
//It will show all data of Students into DataGridView.
SqlCommand cmd = new SqlCommand("SELECT age FROM centre", d);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "centre");
SqlDataReader dr = cmd.ExecuteReader();
//listBox1.DataSource = ds;
DataTable myDataTable = ds.Tables[0];
DataRow tempRow = null;
if (dr.HasRows)
{
foreach (DataRow tempRow_Variable in myDataTable.Rows)
{
tempRow = tempRow_Variable;
listBox1.Items.Add((tempRow["age"]));
//listBox1.Items.Add((tempRow["id"] + " (" + tempRow["name"] + ")" + " (" + tempRow["Country"] + ")" + " (" + tempRow["City"] + ")"));
}
//listBox1.Items.Add(dr["age"].ToString());
}
d.Close();
}