I want to get the relevant name of the product Id into a text box when I select the product id from the combobox

 myConnection = New SqlConnection(connectionstring)
        myConnection.Open()

        myCommand = New SqlCommand(" Select  Name FROM [Product_Name_List] Where P_Id='" & ComboBox2.Text & "'", myConnection)
        Dim dr As SqlDataReader = myCommand.ExecuteReader
        While dr.Read()

            TextBox8.Text = Val(dr(0)).ToString

        End While
        dr.Close()
        myConnection.Close()

What is the problem with above code?

I want to know to what event in the combobox I have to include this coding.......................

when I used that event I get the default value as 0

The default value for what? When you run the following code

Private Sub ComboBox2_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

    myConnection = New SqlConnection(connectionstring)
    myConnection.Open()

    myCommand = New SqlCommand(" Select  Name FROM [Product_Name_List] Where P_Id='" & ComboBox2.Text & "'", myConnection)
    Dim dr As SqlDataReader = myCommand.ExecuteReader

    ComboBox2.Items.Clear

    While dr.Read()
        Debug.WriteLine(dr(0))
        TextBox8.Text = Val(dr(0)).ToString
    End While

    dr.Close()
    myConnection.Close()

End Sub

what is the output in the debug window?

There was no errors
but didn't work as i want..................
when select a item from combox the relevant name of that item didn't display in the textbox.................

Run the following code and post the output from the debug window

Private Sub ComboBox2_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

myConnection = New SqlConnection(connectionstring)
myConnection.Open()

myCommand = New SqlCommand(" Select  Name FROM [Product_Name_List] Where P_Id='" & ComboBox2.Text & "'", myConnection)

Debug.WriteLine(myCommand.CommandText)

Dim dr As SqlDataReader = myCommand.ExecuteReader

ComboBox2.Items.Clear

While dr.Read()
    Debug.WriteLine(dr(0))
    TextBox8.Text = Val(dr(0)).ToString
End While

dr.Close()
myConnection.Close()

End Sub

the textbox which the name should be appear become zero and combox become empty
but there is no errors

How many times are you planning to ignore my request for you to post the output (in the debug window) from your code?

I didn't ignore your request
I didn't get neither error or output then I changed code like this

  TextBox8.Text = Val(dr(0)).ToString

to

  TextBox8.Text = (dr(0)).ToStrin)

then it started work
Thank you very much to you Sorry If I troubled you........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.