Read data from database table add it to a ListBox : DataBinding ListBox « ADO.Net « C# / CSharp Tutorial






using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class ListBoxTableReadingFill : System.Windows.Forms.Form
{
    private System.Data.SqlClient.SqlConnection myConnection;
    private System.Data.DataSet myDataSet;
    private System.Data.SqlClient.SqlCommand myCommand;
    private System.Data.SqlClient.SqlDataAdapter myDataAdapter;
    private System.Windows.Forms.ListBox employeeList;

  private System.ComponentModel.Container components = null;

  public ListBoxTableReadingFill()
  {
    InitializeComponent();
        string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";

        myConnection = new System.Data.SqlClient.SqlConnection(connectionString);
        myConnection.Open();

        myDataSet = new System.Data.DataSet();
        myDataSet.CaseSensitive=true;

        string commandString = "Select ID, FirstName from Employee";


        myCommand = new System.Data.SqlClient.SqlCommand();
        myCommand.Connection=myConnection;
        myCommand.CommandText= commandString;

        myDataAdapter = new SqlDataAdapter();
        myDataAdapter.SelectCommand = myCommand;
        myDataAdapter.TableMappings.Add("Table", "Employee");
        myDataAdapter.Fill(myDataSet);


        DataTable myDataTable = myDataSet.Tables[0];
        
        foreach (DataRow dataRow in myDataTable.Rows)
        {
            employeeList.Items.Add(dataRow["ID"] + ":" + dataRow["FirstName"]  );
        }
    }

  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
        this.employeeList = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // employeeList
        // 
        this.employeeList.Location = new System.Drawing.Point(16, 8);
        this.employeeList.Name = "employeeList";
        this.employeeList.Size = new System.Drawing.Size(216, 147);
        this.employeeList.TabIndex = 0;
        // 
        // ListBoxTableReadingFill
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(464, 205);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.employeeList});
        this.Name = "ListBoxTableReadingFill";
        this.Text = "ListBoxTableReadingFill";
        this.ResumeLayout(false);

    }

  [STAThread]
  static void Main() 
  {
    Application.Run(new ListBoxTableReadingFill());
  }
}








32.54.DataBinding ListBox
32.54.1.Data Binding: ListBoxData Binding: ListBox
32.54.2.Binding ListBox to a Database table
32.54.3.Read data from database table add it to a ListBox
32.54.4.Fill ListBox with data from a DataReader