Windows Phone Developers

Saturday, April 17, 2010

How to Bind DataSet to ListBox using C#

How to add items from database to Listbox control using C#

The following code binds the PlayerDetails table with the listbox

string sConString = GetConnectionString("TeamDBConnectionString");
            SqlConnection Con = new SqlConnection(sConString);
            Con.Open();

            //Command Text
            String sSQLCmdText = "Select * from PlayerDetails";
            
            //Create a new SQL Data Adapter
            SqlDataAdapter DA = new SqlDataAdapter(sSQLCmdText , Con);
            
            DataSet DS = new DataSet("PlayerDS"); //DataSet

            DS.Clear();
            DA.Fill(DS, "Player");

            listBox1.DisplayMember = "PlayerName";
            listBox1.ValueMember = "PlayerID";
            listBox1.DataSource = DS.Tables["Player"];

            Con.Close();

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

6 comments:

  1. this code is not working.....

    ReplyDelete
  2. please update the code, this code isnt working

    ReplyDelete
  3. Thanks! That just exactly what I needed!

    ReplyDelete
  4. remove DS.CLEAR() Line

    then it will work.... and

    you can try this.
    Listbox Binding

    ReplyDelete
    Replies
    1. I know it is correct but i want to add dataset data to the listbox which is already contains data the i want to uppend dataset data to the list box item

      Delete