Mark Fenech

Mark Fenech

  • NA
  • 82
  • 69.2k

Populate Listbox from database

Jun 20 2011 4:13 PM
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)