How to Databind to ListBox
List of detail how to show listbox coding method
asp.net
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
deva nathanPosted Nov 22, 2012, 3:56 AM
{
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();
}