I want when click on the Name Listed on a Listbox from the database to fill Textboxes with Username and Password of particular user from SQL database.
I am able to see Names stored on the database on the listbox but when i click on the name its unable to fill the Textboxes.
Here is the code that fills the Listbox with data from database (NOTE: I am able to view a list of names on the Listbox with the code below)
Public Sub ListBox()
Try
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim cmd As SqlCommand
cmd = con.CreateCommand()
cmd.CommandText = "SELECT * FROM Admin"
Dim Reader As SqlDataReader
Reader = cmd.ExecuteReader
While Reader.Read
lbAdmin.Items.Add(Reader.Item("Full Name"))
End While
Catch ex As Exception
End Try
End Sub
And here is the code I want to fill the Textboxes after clicking on a name in a Listbox (NOTE: Unable to fill Textboxes with code below). I really dont know where I am going wrong, please help.
Private Sub lbAdmin_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lbAdmin.SelectedIndexChanged
Try
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim cmd As SqlCommand
cmd = con.CreateCommand()
cmd.CommandText = "SELECT * FROM Admin"
Dim Reader As SqlDataReader
Reader = cmd.ExecuteReader
While Reader.Read
txtfullname.Text = Reader.GetString("Full Name")
txtusername.Text = Reader.GetString("Username")
txtpassword.Text = Reader.GetString("Password")
End While
Catch ex As Exception
End Try
End Sub