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.