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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » DataBinding ListBox 
32.54.3.Read data from database table add it to a ListBox
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 Disposebool disposing )
  {
    ifdisposing )
    {
      if (components != null
      {
        components.Dispose();
      }
    }
    base.Disposedisposing );
  }
  private void InitializeComponent()
  {
        this.employeeList = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // employeeList
        // 
        this.employeeList.Location = new System.Drawing.Point(168);
        this.employeeList.Name = "employeeList";
        this.employeeList.Size = new System.Drawing.Size(216147);
        this.employeeList.TabIndex = 0;
        // 
        // ListBoxTableReadingFill
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(464205);
        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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.