Store Database data into a ListBox : Database ListBox « Database ADO.net « VB.Net






Store Database data into a ListBox

Store Database data into a ListBox
 Imports System
 Imports System.Drawing
 Imports System.Collections
 Imports System.ComponentModel
 Imports System.Windows.Forms
 Imports System.Data
 Imports System.Data.SqlClient
 
Public Class MainClass

    Shared Sub Main(  )
        Application.Run(New ADOForm1() )
    End Sub

   
End Class

     Public Class ADOForm1
     Inherits System.Windows.Forms.Form

     Private components As System.ComponentModel.Container
     Private lbEmployees As System.Windows.Forms.ListBox

     Public Sub New(  )
         InitializeComponent(  )

         Dim connectionString As String ="server=(local)\SQLEXPRESS;" & _
          "integrated security=sspi;database=MyDatabase"


         Dim commandString As String = _
           "Select FirstName, LastName from Employee"

         Dim myDataAdapter As New SqlDataAdapter( _
           commandString, connectionString)

         Dim myDataSet As New DataSet(  )

         myDataAdapter.Fill(myDataSet, "Employee")

         Dim myDataTable As DataTable = myDataSet.Tables(0)

         Dim tempRow As DataRow
         For Each tempRow In myDataTable.Rows
             lbEmployees.Items.Add((tempRow("FirstName") & _
               " (" & tempRow("LastName") & ")"))
         Next

     End Sub 'New

     Private Sub InitializeComponent(  )
         Me.components = New System.ComponentModel.Container(  )
         Me.lbEmployees = New System.Windows.Forms.ListBox(  )
         lbEmployees.Location = New System.Drawing.Point(48, 24)
         lbEmployees.Size = New System.Drawing.Size(368, 160)
         lbEmployees.TabIndex = 0
         Me.Text = "ADOFrm1"
         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
         Me.ClientSize = New System.Drawing.Size(464, 273)
         Me.Controls.Add(lbEmployees)
     End Sub 'InitializeComponent

 End Class 'ADOForm1
           
       








Related examples in the same category

1.Read Access Data table and store data in ListBoxRead Access Data table and store data in ListBox