troubleshooting Question

How do I insert/update a mysql database from the vb .net datagrid

Avatar of rchrdrea
rchrdrea asked on
Visual Basic.NET
2 Comments1 Solution1212 ViewsLast Modified:
I have a mysql database.

I have made an interface that populates a datagrid (DataGrid1) with data from the mysql database.

I would like to insert new data into the interface and have it insert or update data to the database depending on what button was pushed.

Ignore the myschema parts...I was attemtping to try things that failed.  I think I might have to get hold of parameters and figure out how to make the insert statement understand that they need to use the boxes from the datagrid.  How woudl the program know which one I want to insert.  The datagird shows all form the source and it could theoretially insert old data in the wrong place.  Do I have to look for what is changed first in the datagrid to identify what to insert?

Thanks for any help you can lend.

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        Dim da As New MySql.Data.MySqlClient.MySqlDataAdapter
        myConnectionString = "data source=localhost;" _
            & "uid=root;" _
            & "pwd=******;" _
            & "database=**********;"
        Dim conn As New MySql.Data.MySqlClient.MySqlConnection(myConnectionString)
        da.MissingSchemaAction = MissingSchemaAction.Add
            da.InsertCommand = New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO checkbook_tbl (entry,chk,location,debit,credit,balance,category,reconciled) values (@id,@chk,@location,@debit,@credit,@balance,@category,@reconciled)", conn)
        Catch ex As MySql.Data.MySqlClient.MySqlException
            Select Case ex.Number
                Case 0
                    MessageBox.Show("Cannot connect to server. Contact administrator")
                Case 1045
                    MessageBox.Show("Invalid username/password, please try again")
                Case Else
            End Select
        End Try
    End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros