Hi i have a combobox and i need to populate it with the name of the publisher. From the table i have 2 columns id and name. What am I doing wrong ?
public void populateComboBox(ComboBox cmb)
{
cmd = new SqlCommand("SELECT Name FROM tbl_publisher", sqlConn);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
SqlCommandBuilder sqb = new SqlCommandBuilder(da);
da.Fill(ds, "tbl_publisher");
foreach (DataRow myRow in ds.Tables["tbl_publisher"].Rows)
{
//string publish = (string)myRow["Name"];
cmb.Items.Add(ds.Tables["tbl_publisher"].Rows[0]["Name"].ToString());
}
}
Loading
VulpesPosted Jun 20, 2011, 4:48 PM
Pradeep ChandrakerPosted Jun 20, 2011, 4:33 PM
public void populateComboBox(ComboBox cmb)
{
cmd = new SqlCommand("SELECT Name, Id FROM tbl_publisher", sqlConn);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
SqlCommandBuilder sqb = new SqlCommandBuilder(da);
da.Fill(ds, "tbl_publisher");
foreach (DataRow myRow in ds.Tables["tbl_publisher"].Rows)
{
cmb.Items.Add(new ListItem(myRow ["Name"].ToString(), myRow ["Id"].ToString()));
}
}