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

Imports MySql.Data.MySqlClient



Public Class Form1
    Dim str As String
    Dim conn As MySqlConnection
    Dim cmd As MySqlCommand


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try

            str = " host = localhost ; username = 'root'; password = 'root'; database= asiawise; pooling = false;"
            conn = New MySqlConnection(str)
            conn.Open()

            cmd.CommandText = "INSERT INTO students(last_name,date_enrolled ,first_name ,category ,age ,level,mentor,schedule) VALUES('textbox1.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
End Class

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

Imports MySql.Data.MySqlClient



Public Class Form1
    Dim str As String
    Dim conn As MySqlConnection
    Dim cmd As MySqlCommand


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try

            str = " host = localhost ; username = 'root'; password = 'root'; database= asiawise; pooling = false;"
            conn = New MySqlConnection(str)
            conn.Open()

            cmd.CommandText = "INSERT INTO students(last_name,date_enrolled ,first_name ,category ,age ,level,mentor,schedule) VALUES('textbox1.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
End Class

Dim conn As MySqlConnection 'Should be Dim conn as New MySqlConnection
Dim cmd As MySqlCommand 'same as above use New


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

str = " host = localhost ; username = 'root'; password = 'root'; database= asiawise; pooling = false;"
conn = New MySqlConnection(str) ' HERE use conn.connectionstring = str
conn.Open()

Hi everettnewell,
have you thought of using the "CommandBuilder" command. The syntax of which for your problem would look something like:-

Imports MySql.Data.MySqlClient



Public Class Form1
    Dim str As String
    Dim conn As MySqlConnection
    Dim dr As DataRow
    Dim ds As DataSet
    Dim da As DataAdapter
    Dim cBuilder As CommandGuilder
    Dim sqlString As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try

            str = " host = localhost ; username = 'root'; password = 'root'; database= asiawise; pooling = false;"
            conn = New MySqlConnection(str)
            conn.Open()

            ' This next line will create a table with no rows in it.
            sqlString = "SELECT * From students WHERE age = 999"
            da = New MySqlDataAdapter(sqlString, conn)
            ds = New DataSet()
            da.Fill(ds, "Students")
            dr = ds.Tables("Students").NewRow()

            dr.Item("last_name") = textbox1.text
            dr.Item("date_enrolled") = textbox3.text
            dr.Item("first_name") = textbox2.text
            dr.Item("category") = ComboBox1.text
            dr.Item("age") = 'textbox4.text'
            dr.Item("level") = ComboBox2.text
            dr.Item("mentor") = textbox5.text
            dr.Item("schedule") = ComboBox3.text

            cBuilder = New MySqlCommandBuilder(da)
            da.InsertCommand = cBuilder.GetInsertCommand()
            da.Update(ds)

            conn.Close()

        Catch ex As Exception
            MessageBox.Show("Connection has timedout.")

        End Try

    End Sub
End Class

I know this code works, i use this in one of my projects. I know that there is more code than you had originally, but i believe it will be a small price to pay if you can save the data.

Good Luck !

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.