populate listbox c# from sql
  • how can i populate listbox in c# from sql database.
  • 1 Comment sorted by
  • use below code

    SqlConnection objCon = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest;Integrated Security=True");

                SqlCommand objCmd = new SqlCommand("select ID,CountryName from tblCountry", objCon);

                SqlDataAdapter objDA = new SqlDataAdapter(objCmd);

                objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
                DataTable dt = new DataTable();

                objDA.Fill(dt);

                listBox1.DataSource = dt;

                //this column1 will display as text
                listBox1.DisplayMember = "CountryName";

                //this column2 will use as back end value who can you use in selectedValue property
                listBox1.ValueMember = "ID";


Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

    Optimum Creative