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

Recommended Answers

Before you do anything else, encrypt those passwords. Your code shows a classic blunder in name and password databases. The good news is there are discussions about that with this search: https://www.google.com/search?q=vb.net+one+way+password+encryption&gl=US

Because such passwords must not be …

Jump to Post

About the lbAdmin_SelectedIndexChanged method. It looks incorrect from here. You have the index and should use that to get the name you are interested in. But your handler seems to read the entire database rather than a one line SQL command like SELECT * FROM Admin where Username = your …

Jump to Post

PS. A comment about "SELECT * FROM Admin"

A common lesson from many SQL books start off with SELECT which is a bad way to learn about the SELECT command. Unless you need all columns do not use . Call out the column or columns you need. Example readings …

Jump to Post

All 4 Replies

Before you do anything else, encrypt those passwords. Your code shows a classic blunder in name and password databases. The good news is there are discussions about that with this search: https://www.google.com/search?q=vb.net+one+way+password+encryption&gl=US

Because such passwords must not be stored in the clear you have to consider what this apps is for. If it's for you and storing your own passwords you could at least encrypt it in a database. If this is for a customer or client system then you never hold their passwords like this. You use the one way system so you can never show the user passwords.

commented: Thank you for that observation plus suggestion, I will surely look onto that before I publish the app. +0

About the lbAdmin_SelectedIndexChanged method. It looks incorrect from here. You have the index and should use that to get the name you are interested in. But your handler seems to read the entire database rather than a one line SQL command like SELECT * FROM Admin where Username = your indexed name from the list.

commented: I am a newbie to programming, the app I am developing is a first one. I know I have a long way to go, I will try by all means to adhere to your advise +0

PS. A comment about "SELECT * FROM Admin"

A common lesson from many SQL books start off with SELECT which is a bad way to learn about the SELECT command. Unless you need all columns do not use . Call out the column or columns you need. Example readings from the web: https://www.google.com/search?q=SELECT+*+is+bad

As your database grows, you will see the performance hit.

commented: Kindly post example so that I know where and how to go round the problem. Thank you for your responses. +0

The issue has been resolved and below is the solution, perhaps it may help someone in the near future:

Dim cmd As SqlCommand
        cmd = con.CreateCommand()
        cmd.CommandText = "SELECT * FROM Admin where [Full Name]=@Name"
        cmd.Parameters.AddWithValue("@name", lbAdmin.SelectedItem.ToString)
        Dim Reader As SqlDataReader
        Reader = cmd.ExecuteReader
        While Reader.Read
            txtfullname.Text = Reader("Full Name")
            txtusername.Text = Reader("Username")
            txtpassword.Text = Reader("Password")

Thank you so much rproffitt for your responses, please keep up the spirit.

Kind regards,
Speed Ack.

About the SELECT * area. In your first code passage you don't need all the columns. I would be guessing the column name but it could be "Full Name" so the SELECT would be SELECT "Full Name".

In the future as you become a SQL guru one of the things you always look for when there are performance complaints is the old SELECT *.