We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,440 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to filter a value into a textbox from combox

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()
3
Contributors
11
Replies
4 Days
Discussion Span
6 Months Ago
Last Updated
12
Views
Question
Answered
Dili1234
Junior Poster
122 posts since Aug 2012
Reputation Points: -2
Solved Threads: 4
Skill Endorsements: 2

What is the problem with above code?

Pgmer
Practically a Posting Shark
881 posts since Apr 2008
Reputation Points: 60
Solved Threads: 158
Skill Endorsements: 1

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

Dili1234
Junior Poster
122 posts since Aug 2012
Reputation Points: -2
Solved Threads: 4
Skill Endorsements: 2

You can use SelectedIndexChanged

Reverend Jim
Illigitimae non carborundum
Moderator
3,743 posts since Aug 2010
Reputation Points: 585
Solved Threads: 470
Skill Endorsements: 33

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

Dili1234
Junior Poster
122 posts since Aug 2012
Reputation Points: -2
Solved Threads: 4
Skill Endorsements: 2

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?

Reverend Jim
Illigitimae non carborundum
Moderator
3,743 posts since Aug 2010
Reputation Points: 585
Solved Threads: 470
Skill Endorsements: 33

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

Dili1234
Junior Poster
122 posts since Aug 2012
Reputation Points: -2
Solved Threads: 4
Skill Endorsements: 2

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

Reverend Jim
Illigitimae non carborundum
Moderator
3,743 posts since Aug 2010
Reputation Points: 585
Solved Threads: 470
Skill Endorsements: 33

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

Dili1234
Junior Poster
122 posts since Aug 2012
Reputation Points: -2
Solved Threads: 4
Skill Endorsements: 2

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

Reverend Jim
Illigitimae non carborundum
Moderator
3,743 posts since Aug 2010
Reputation Points: 585
Solved Threads: 470
Skill Endorsements: 33
Question Answered as of 6 Months Ago by Reverend Jim and Pgmer

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

Dili1234
Junior Poster
122 posts since Aug 2012
Reputation Points: -2
Solved Threads: 4
Skill Endorsements: 2

No trouble. Glad you got it working.

Reverend Jim
Illigitimae non carborundum
Moderator
3,743 posts since Aug 2010
Reputation Points: 585
Solved Threads: 470
Skill Endorsements: 33

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1969 seconds using 2.71MB