Retrieve data from SQL server database to form controls by clicking on a list box using visual basic

Hossam 40 Reputation points
2023-03-25T07:40:07.5133333+00:00

Hi I need a code to Retrieve data from SQL server database to form controls by clicking on a list box using visual basic, list box already populated but not able to retrieve data by clicking on a list box to fill other form controls such as textbox and combobox

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,528 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Ayomide Oluwaga 941 Reputation points
    2023-03-26T01:46:52.8566667+00:00

    Sure Hossam, the following code will retrieve the data from your SQL Server database and populate the TextBox control when an item in the ListBox is selected:

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        ' Retrieve the selected item from the ListBox
        Dim selectedItem As String = ListBox1.SelectedItem.ToString()
        
        ' Query the database for the corresponding data
        Dim connectionString As String = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
        Dim queryString As String = "SELECT Column1, Column2 FROM MyTable WHERE Column3 = @SelectedValue;"
        
        Using connection As New SqlConnection(connectionString)
            connection.Open()
            Using command As New SqlCommand(queryString, connection)
                command.Parameters.AddWithValue("@SelectedValue", selectedItem)
                Dim reader As SqlDataReader = command.ExecuteReader()
                While reader.Read()
                    ' Populate the TextBox control with the retrieved data
                    TextBox1.Text = reader("Column1").ToString()
                    ' Similarly, you can populate other controls such as ComboBoxes here
                End While
            End Using
        End Using
    End Sub
    
    

    Note that you'll need to modify the SQL Server connection string and query to match your database schema and table/column names. Additionally, you may need to add error handling code in case the query fails or no data is returned.

    0 comments No comments

  2. Hossam 40 Reputation points
    2023-03-26T07:13:11.2866667+00:00

    @Ayomide Oluwaga Thank you very much for your help but unfortunately the code didn't populate the textbox and also no error message (nothing happens when I select any item in the listbox, here is the code after I adjusted to my database

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    
            ' Retrieve the selected item from the ListBox
            Dim selectedItem As String = ListBox1.SelectedItem.ToString()
    
            ' Query the database for the corresponding data
            Dim connectionString As String = "Data Source=udse;Initial Catalog=UDSEDB;Integrated Security=True"
            Dim queryString As String = "SELECT tel, email FROM invdata WHERE invnm = @SelectedValue;"
    
            Using connection As New SqlConnection(connectionString)
                connection.Open()
                Using command As New SqlCommand(queryString, connection)
                    command.Parameters.AddWithValue("@SelectedValue", selectedItem)
                    Dim reader As SqlDataReader = command.ExecuteReader()
                    While reader.Read()
                        ' Populate the TextBox control with the retrieved data
                        TextBox1.Text = reader("email").ToString()
                        ' Similarly, you can populate other controls such as ComboBoxes here
                    End While
                End Using
            End Using
        End Sub
    

  3. Sedat SALMAN 12,820 Reputation points
    2023-03-26T08:28:33.4166667+00:00

    Add some debug and error handling code to your code since it seems correct

    
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            ' Retrieve the selected item from the ListBox
            Dim selectedItem As String = ListBox1.SelectedItem.ToString()
    
            ' Query the database for the corresponding data
            Dim connectionString As String = "Data Source=udse;Initial Catalog=UDSEDB;Integrated Security=True"
            Dim queryString As String = "SELECT tel, email FROM invdata WHERE invnm = @SelectedValue;"
    
            Using connection As New SqlConnection(connectionString)
                connection.Open()
                Using command As New SqlCommand(queryString, connection)
                    command.Parameters.AddWithValue("@SelectedValue", selectedItem)
                    Dim reader As SqlDataReader = command.ExecuteReader()
                    If reader.HasRows Then
                        While reader.Read()
                            ' Populate the TextBox control with the retrieved data
                            TextBox1.Text = reader("email").ToString()
                            ' Similarly, you can populate other controls such as ComboBoxes here
                        End While
                    Else
                        MessageBox.Show("No data found for the selected item.", "Data Not Found", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                End Using
            End Using
        Catch ex As Exception
            MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    
    

  4. Hossam 40 Reputation points
    2023-03-26T08:55:46.6833333+00:00

    @Ayomide Oluwaga, I copied your code but I gives a message the no data fount although the data is there, please help.


  5. Sedat SALMAN 12,820 Reputation points
    2023-03-26T13:46:55.09+00:00

    my friend according to the message there are no rows returned as a result of the SQL query please check your query and parameters maybe you need to check extra spaces etc.

    0 comments No comments