Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to Populate a ListBox Control with Values from an SQL Server Express Database Table

Avatar of FaheemAhmadGul
FaheemAhmadGulFlag for United Kingdom of Great Britain and Northern Ireland asked on
.NET ProgrammingVisual Basic.NETMicrosoft SQL Server 2005
3 Comments1 Solution463 ViewsLast Modified:
I have ListBox1 Control on a Visual Studio 2008 Project in Visual Basic.  
I need help with code for the initialise event of Form1 which will populate this ListBox1 with Values (in alphabetical order) from the ClientName field of all records in a table named ClientNames) in an SQL Server Express Edition 2005 Database.

The connection string for connecting to my SQL Server Express Database is as follows:
ConnectionString = "Data Source=VISTA-ON-MAC\SQLEXPRESS;Initial Catalog=WriterDemo;Persist Security Info=True;User ID=sa;Password=peter;Connect Timeout=30"


Many thanks for your help.
Please note I am using Visual Studio 2008 for a Visual Studio 2008 Project.

The code that I am providing in the Code window pulls one ClientName from ClientNames Table into a textbox  named txtNotes on a Form  in my project.

The code that solved my previous problem on this theme is here:
https://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26206780.html
However my previous question on this theme was about filling a listbox with Data from an Access Database.


Many thanks for your help in anticipation.

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class ViewClientNames

    Dim sqlConn As New SqlConnection
    Private mySQLConnection As New SqlConnection
    Private mySQLDataAdapter As SqlDataAdapter
    Private mySQLCommandBuilder As SqlCommandBuilder
    Private myDataTable As New DataTable
    Private RowPosition As Integer
    Private Sub ViewClientNames_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        mySQLConnection.Close()
        mySQLConnection.Dispose()
    End Sub
    Private Sub ViewClientNames_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mySQLConnection.ConnectionString = "Data Source=VISTA-ON-MAC\SQLEXPRESS;Initial Catalog=WriterDemo;Persist Security Info=True;User ID=sa;Password=peter;Connect Timeout=30"
        mySQLConnection.Open()
        mySQLDataAdapter = New SqlDataAdapter("SELECT note FROM ClientNames", mySQLConnection)
        mySQLCommandBuilder = New SqlCommandBuilder(mySQLDataAdapter)
       
        mySQLDataAdapter.Fill(myDataTable)
        Me.ShowCurrentRecord()
    End Sub
    Private Sub ShowCurrentRecord()
        If myDataTable.Rows.Count = 0 Then
            txtNotes.Text = ""
            Exit Sub
        End If
        txtNotes.Rtf = myDataTable.Rows(RowPosition)("ClientName").ToString()
       

    End Sub
ASKER CERTIFIED SOLUTION
Avatar of tickett
tickettFlag of United Kingdom of Great Britain and Northern Ireland image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers