Windows Phone Developers

Showing posts with label C# Listbox and SQL Server. Show all posts
Showing posts with label C# Listbox and SQL Server. Show all posts

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

Monday, April 5, 2010

How to Bind a Lisbox to Database in C# (.NET)

Bind Listbox to Dataset using Visual Studio

Following steps would help you in connected a database/dataset to the list box

Step1: Add a List Box control to the form and select the DataSource from quick edit

Step 2: Select appropriate data source type (We are going to connect to an SQL Server DB)


Step 3: Select Appropriate Database Model


Step 4: Add a connection to database and select appropriate DB

Step 5: Select appropriate field you want to show in listbox


A dataset will be created and will be used for it. You can write necessary code in listbox selectionchange events depending upon the business logic 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