2
Reply

Populate Listbox from database

Mark Fenech

Mark Fenech

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