1.11M Members

How to filter a value into a textbox from combox

 
0
 

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()
 
0
 

What is the problem with above code?

 
0
 

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

 
0
 

You can use SelectedIndexChanged

 
0
 

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

 
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?

 
0
 

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.................

 
0
 

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

 
0
 

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

 
0
 

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

Question Answered as of 1 Year Ago by Reverend Jim and Pgmer
 
0
 

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........

 
0
 

No trouble. Glad you got it working.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article