2
Answers

Populate Listbox from database

Mark Fenech

Mark Fenech

13y
7.5k
1
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());
            }
        }
Answers (2)