I have a datagridview in which the user can enter data into multiple new rows. What I want the app to do is when the user clicks on the Save button, it will save the multiple new rows and insert the new rows in the database, where the datagridview gets the data from. Thanks in advance.
Here is my code for what I have so far:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sConnString As String = "Provider=SQLOLEDB;Data Source=tripleX; Database=MyClaims;"
Dim dgv_sql As String = "INSERT INTO " + ComboBox1.SelectedValue + " (CODE, SDESCR, LDESCR, STARTDT, ENDDT) VALUES (?,?,?,?, ?)"
Dim connString As New OleDbConnection(sConnString)
Dim cmd As OleDbCommand = New OleDbCommand(dgv_sql, connString)
Dim objDataAdapter As New OleDbDataAdapter
Dim strCode As String
Dim strSDescr As String
Dim strLDescr As String
Dim strStartDate As String
Dim strEndDate As String
Dim counter As Integer
connString.Open()
For counter = 0 To (dgv_Investigation.Rows.Count - 1)
strCode = dgv_Investigation.SelectedRows(counter).Cells("CODE").Value
strSDescr = dgv_Investigation.SelectedRows(counter).Cells("SDESCR").Value
strLDescr = dgv_Investigation.SelectedRows(counter).Cells("LDESCR").Value
strStartDate = dgv_Investigation.SelectedRows(counter).Cells("STARTDT").Value
strEndDate = dgv_Investigation.SelectedRows(counter).Cells("ENDDT").Value
cmd.ExecuteNonQuery()
objDataAdapter.InsertCommand = cmd
cmd.CommandText = "INSERT INTO " + ComboBox1.SelectedValue + " (CODE, SDESCR, LDESCR, STARTDT, ENDDT) VALUES (?,?,?,?,?)"
cmd.Parameters.Add(strCode)
cmd.Parameters.Add(strSDescr)
cmd.Parameters.Add(strLDescr)
cmd.Parameters.Add(strStartDate)
cmd.Parameters.Add(strEndDate)
Next
connString.Close()
BTW I am using VB.NET 2005