Question Connection has timedout.!!! to mySQL

bellil

Member
Joined
Sep 19, 2011
Messages
10
Programming Experience
1-3
Hi All,
ho do i store information to a mysql database? what i am trying to do is to fill out some students information and when i try to click save it should be saved to a mysql database.... here is my code but every time i click save it couldn't connect to the database.... whats wrong with my code? thanks guys

VB.NET:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try

            str = "Database=pcs;" & _
                    "Data Source=127.0.0.1;" & _
                    "User Id=root;Password=awardsw;" & _
                    "Connection Timeout=20"
            conn.ConnectionString = str
            conn.Open()

            cmd.CommandText = "INSERT INTO computers(computernumber,computertype,Factory,serialnumber,PartNumber,RAM,HardDisk,HDDSN,CDDVD,CDDVDSN,VGACARD,VGARAM,KeyboardType,MouseType,MonitorType,MoreInfo,Repairinfo) VALUES('ComputernumberTextBox.Text','textbox3.text','textbox2.text','ComboBox1.text','textbox4.text','ComboBox2.text','textbox5.text','ComboBox3.text');"

            cmd.Connection = conn

            cmd.ExecuteNonQuery()
            conn.Close()
        Catch ex As Exception
            MessageBox.Show("Connection has timedout.")

        End Try

    End Sub
 
You are catching any and all exceptions and then telling the user that the connection has timed out without any idea if that's actually what happened. The exception is there to tell you what went wrong. Don't ignore it. Once you know what happened, then you can go about fixing the problem.

Apart from that, you are not going to save the data you want with that code. Your SQL code contains all text literals and it's that literal text that you will save, not the contents of the controls with those names. Follow the Blog link in my signature and check out my post on ADO.NET Parameters to learn how to insert values into a SQL statement.
 
@jmcilhinney

Thank you for your help
Okay ... now i'm looking for how put the value of textbox inside the field computernumber

VB.NET:
 cmd.CommandText = "INSERT INTO computers(computernumber) VALUES('ComputernumberTextBox.Text');"

BR,
 
Hi,

i made this line and i have an error :

cmd.CommandText = "INSERT INTO computers(computernumber) VALUES(' " & ComputernumberTextBox.Text & " ');"

the error is : Column count doesn't match value count at row 1
the line of error is : cmd.ExecuteScalar()

may now you can help me?

BR,
 
I told you in my previous post to follow the Blog link in my signature and read about why and how to use ADO.NET parameters. You have either ignored that advice or ignored the information contained in that post.
 
Back
Top