Private Sub PopulateProductList()
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strSQL As String
Dim objListItem As ListItem
Try
strSQL = "SELECT [Sr No], [First Name] FROM [Lani]"
con = New OleDbConnection(connectionString)
con.Open()
cmd = New OleDbCommand(strSQL, con)
dr = cmd.ExecuteReader()
lblani.Items.Clear()
Do While dr.Read()
objListItem = New ListItem(dr.Item("First Name").ToString(), _ CInt(dr.Item("Sr No"))) lblani.Items.Add(objListItem)
Loop
If lblani.Items.Count > 0 Then
lblani.SetSelected(0, True)
End If
' Close and Clean up objects
dr.Close()
con.Close()
cmd.Dispose()
con.Dispose()
Catch ex As OleDbException
MsgBox(ex.Message.ToString) End TryPrivate Sub PopulateProductList()
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strSQL As String
Dim objListItem As ListItem
Try
strSQL = "SELECT [Sr No], [First Name] FROM [Lani]"
con = New OleDbConnection(connectionString)
con.Open()
cmd = New OleDbCommand(strSQL, con)
dr = cmd.ExecuteReader()
lblani.Items.Clear()
Do While dr.Read()
objListItem = New ListItem(dr.Item("First Name").ToString(), _
CInt(dr.Item("Sr No")))
lblani.Items.Add(objListItem)
Loop
If lblani.Items.Count > 0 Then
lblani.SetSelected(0, True)
End If
' Close and Clean up objects
dr.Close()
con.Close()
cmd.Dispose()
con.Dispose()
Catch ex As OleDbException
MsgBox(ex.Message.ToString)
End Try
And the following sub routine is load in the Listbox_selectedindexchanged event
Syntax (Toggle Plain Text)
Private Sub PopulateForm()
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strSQL As String
Dim objListItem As ListItem
' Dim strID As String
Try
objListItem = CType(lblani.SelectedItem, ListItem)
strSQL = ("Select * from [Lani] Where [Sr No] = " & txtid.Text)
con = New OleDbConnection(connectionString)
con.Open()
cmd = New OleDbCommand(strSQL, con)
dr = cmd.ExecuteReader()
If dr.Read() Then
' Populate form with the data in textbox
txtid.Text = dr.Item("Sr No").ToString()
txtfirst.Text() = dr.Item("First Name").ToString() txtmiddle.Text() = dr.Item("Middle Name").ToString() txtlast.Text() = dr.Item("Last Name").ToString() txtcomp.Text() = dr.Item("Company Name").ToString() End If ' Close and Clean up objects dr.Close() con.Close() cmd.Dispose() con.Dispose() Catch e As Exception MsgBox(e.Message, MsgBoxStyle.Critical, "General Error") End Try End Sub End Sub Private Sub PopulateForm()
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strSQL As String
Dim objListItem As ListItem
' Dim strID As String
Try
objListItem = CType(lblani.SelectedItem, ListItem)
strSQL = ("Select * from [Lani] Where [Sr No] = " & txtid.Text)
con = New OleDbConnection(connectionString)
con.Open()
cmd = New OleDbCommand(strSQL, con)
dr = cmd.ExecuteReader()
If dr.Read() Then
' Populate form with the data in textbox
txtid.Text = dr.Item("Sr No").ToString()
txtfirst.Text() = dr.Item("First Name").ToString()
txtmiddle.Text() = dr.Item("Middle Name").ToString()
txtlast.Text() = dr.Item("Last Name").ToString()
txtcomp.Text() = dr.Item("Company Name").ToString()
End If
' Close and Clean up objects
dr.Close()
con.Close()
cmd.Dispose()
con.Dispose()
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
End Try
End Sub
End Sub
And listitem class
Help with Code Tags Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Public Class ListItem Private mValue As String ' Stores a named description of the item Private mID As Integer ' Stores a primary key to the record Public Sub New(ByVal strValue As String, ByVal intID As Integer) mValue = strValue mID = intID End Sub Public Sub New() mValue = "" mID = 0 End Sub Property ID() As Integer Get Return mID End Get Set(ByVal Value As Integer) mID = Value End Set End Property Property Value() As String Get Return mValue End Get Set(ByVal Value As String) mValue = Value End Set End Property Public Overrides Function ToString() As String Return mValue End FunctionEnd ClassPublic Class ListItem
Private mValue As String ' Stores a named description of the item
Private mID As Integer ' Stores a primary key to the record
Public Sub New(ByVal strValue As String, ByVal intID As Integer)
mValue = strValue
mID = intID
End Sub
Public Sub New()
mValue = ""
mID = 0
End Sub
Property ID() As Integer
Get
Return mID
End Get
Set(ByVal Value As Integer)
mID = Value
End Set
End Property
Property Value() As String
Get
Return mValue
End Get
Set(ByVal Value As String)
mValue = Value
End Set
End Property
Public Overrides Function ToString() As String
Return mValue
End Function
End Class
And subsequently I want to Design the select query by adding the three column i.e. First Name, Middle Name and Last Name in the Listbox and by selecting the list item that will show in text box.
Thanks in advance for helping me.