?
Solved

How to input 'MySqlDataReader' into text box?

Posted on 2009-04-25
3
Medium Priority
?
408 Views
Last Modified: 2013-12-27
I am trying to get the code below to input the data read into a text box as oppose to writing to a console as is currently done below.

I imagine this is straight forward so any ideas?

(code found from http://dev.mysql.com/doc/refman/5.0/en/connector-net-examples-mysqldatareader.html#connector-net-examples-mysqldatareader-getbytes)


Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
    Dim myConnection As New MySqlConnection(myConnString)
    Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As MySqlDataReader
    myReader = myCommand.ExecuteReader()
    ' Always call Read before accessing data.
    While myReader.Read()
        Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))
    End While
    myReader.Close()
    myConnection.Close()
End Sub 'ReadMyData

Open in new window

0
Comment
Question by:smotbd
  • 2
3 Comments
 
LVL 3

Expert Comment

by:Aleksei_Malkov
ID: 24234659
Here is your modified code. textBox1 - is the name of text box you'd like to read text into. It assumes you have the form with the textBox.
If you'd like to read as mulitple lines, leave code as is, but don't forget to set Multiline property of textBox to true.
If you'd like to have single line only textBox - remove the line with vbCrLf symbol
        Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
        Dim myConnection As New MySqlConnection(myConnString)
        Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
        myConnection.Open()
        Dim myReader As MySqlDataReader
        myReader = myCommand.ExecuteReader()
        ' Always call Read before accessing data.
        Dim textForTextBox As String
        textForTextBox = String.Empty
        While myReader.Read()
            textForTextBox += (myReader.GetInt32(0) & ", " & myReader.GetString(1))
            textForTextBox += vbCrLf
        End While
        myReader.Close()
        myConnection.Close()
        ' Set the text to control
        textBox1.Text = textForTextBox

Open in new window

0
 

Author Comment

by:smotbd
ID: 24236207
Have tried the code but unfortunately I get an error popup saying: "FormatException"
I am trying to read a character string from my database. Any ideas on how to solve the exception? Not quite sure what myReader.GetInt32(0) and myReader.GetString(1) actually does!
0
 
LVL 3

Accepted Solution

by:
Aleksei_Malkov earned 1500 total points
ID: 24236268
I'm not that good at VB syntax, here is my mistake (no += operation)
and because of format use
myReader.GetInt32(0).ToString()    

textForTextBox = textForTextBox & (myReader.GetInt32(0).ToString() & ", " & myReader.GetString(1))
textForTextBox = textForTextBox & vbCrLf

myReader.GetInt32(0) - returns value from OrderID as Integer
myReader.GetString(1) - returns value from CustomerID as string.

in these 2 string we concatenate them into one string
     
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the most popular question in our Windows Mobile Programming zone is about the backlight - we all want to keep it on when our application is running. Few years ago the function SystemIdleTimerReset() did this job. On our side we had to detec…
With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question