I am having trouble constructing code that will enable me to insert gridview data into mysql database with on duplicate key update sql statement.
The gridview is just a normal gridview which is connected to a browse and upload button.This enables me to upload excel spread sheets to my gridview - which is not databound.
I have done this in VB but am finding it an arduous task writing it in vb.net. Here is the code in VB.
Dim connectionString As String = "Server=*****;Database=****;Uid=****;Pwd=r*****;allow user variables=true"
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
       Handles Button2.Click
    Dim i As Integer = 0
    Dim student id,name,age,adress dbSQL_query As String
    Dim dbSQL_cmd As New SqlClient.SqlCommand
    Dim dbSQL_con As New SqlClient.SqlConnection
    Using SQLConnection As New MySqlConnection(connectionString)
        SQLConnection.Open()
        For i = 0 To DataGridView1.RowCount - 1
            Using sqlCommand As New MySqlCommand()
                student id = DataGridView1.Item(0, i).Value.ToString
                name = DataGridView1.Item(1, i).Value.ToString
                age = DataGridView1.Item(2, i).Value.ToString
               adress = DataGridView1.Item(3, i).Value.ToString.ToLower
                dbSQL_query = "INSERT INTO student)  VALUES  (" + addQoute(student_id) + ", " + _
                      addQoute(name) + ", " + _
                      addQoute(age) + ", " + _
                      addQoute(adress) + ", " + _
                      addQoute(status) + ") on duplicate key update name=(" + addQoute(name) + "),age=(" + addQoute(age) + "),adress=(" + addQoute(adress) + ")"
                With sqlCommand
                    .CommandText = dbSQL_query
                    .Connection = SQLConnection
                    .CommandType = CommandType.Text
                End With
                sqlCommand.ExecuteNonQuery()
                dbSQL_cmd = New SqlClient.SqlCommand(dbSQL_query, dbSQL_con)
            End Using
        Next
        'MessageBox.Show(DataGridView1.RowCount & " student details has been inserted")
        SQLConnection.Close()
    End Using
End Sub
Public Function addQoute(ByVal str As String) As String
    str = Trim(str)
    Return "'" + str + "'"
End Function
 
     
    