I need to modify the simple insert command code here
it is Effective But I need to add a condition which is: Add only the rows where the column 4 is checked
Note: the type of Column 4 is DataGridViewCheckBoxColumn
thank you
PrivateSub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click |
Dim connectionString AsString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DataGridSaveDB.accdb;Persist Security Info=True" |
' If you are using Sql server replace "OleDb" with "Sql" for example |
' OleDBConnection --> SqlConnection |
' OleDbCommand --> SqlCommand |
Using connection AsNew OleDbConnection(connectionString) |
Dim cmdText AsString = "INSERT INTO DGV (ColumnA, ColumnB, ColumnC) VALUES (@ColumnA, @ColumnB, @ColumnC)" |
Dim command AsNew OleDbCommand(cmdText, connection) |
command.Parameters.Add(New OleDbParameter("@ColumnA", OleDbType.VarWChar)) |
command.Parameters.Add(New OleDbParameter("@ColumnB", OleDbType.VarWChar)) |
command.Parameters.Add(New OleDbParameter("@ColumnC", OleDbType.VarWChar)) |
connection.Open() |
Dim transaction As OleDbTransaction = connection.BeginTransaction() |
command.Transaction = transaction |
Try |
For i AsInteger = 0 To DataGridView2.Rows.Count - 2 |
command.Parameters("@ColumnA").Value = DataGridView2.Rows(i).Cells(0).FormattedValue |
command.Parameters("@ColumnB").Value = DataGridView2.Rows(i).Cells(1).FormattedValue |
command.Parameters("@ColumnC").Value = DataGridView2.Rows(i).Cells(2).FormattedValue |
command.ExecuteNonQuery() |
Next i |
transaction.Commit() |
Catch ex As Exception |
Try |
transaction.Rollback() |
Catch rollBackEx As Exception |
MessageBox.Show(rollBackEx.Message) |
EndTry |
EndTry |
End Using |
EndSub |
it is Effective But I need to add a condition which is: Add only the rows where the column 4 is checked
Note: the type of Column 4 is DataGridViewCheckBoxColumn
thank you