Question I need to modify the simple insert command code here

ebrahim

Member
Joined
Jul 13, 2011
Messages
8
Programming Experience
Beginner
I need to modify the simple insert command code here

PrivateSub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
' If you are using Sql server replace "OleDb" with "Sql" for example
' OleDbCommand --> SqlCommand
Dim command AsNew OleDbCommand(cmdText, connection)
command.Parameters.Add(New OleDbParameter("@ColumnB", OleDbType.VarWChar))
Dim transaction As OleDbTransaction = connection.BeginTransaction()
command.Parameters("@ColumnA").Value = DataGridView2.Rows(i).Cells(0).FormattedValue
command.Parameters("@ColumnC").Value = DataGridView2.Rows(i).Cells(2).FormattedValue
Next i
transaction.Commit()
transaction.Rollback()
Catch rollBackEx As Exception
EndTry
EndTry
End Using
EndSub

[TD="bgcolor: #F7F7F7"] Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DataGridSaveDB.accdb;Persist Security Info=True" [/TD]

[TD="bgcolor: #F7F7F7"] ' OleDBConnection --> SqlConnection [/TD]

[TD="bgcolor: #F7F7F7"] Using connection As New OleDbConnection(connectionString) [/TD]

[TD="bgcolor: #F7F7F7"] Dim cmdText As String = "INSERT INTO DGV (ColumnA, ColumnB, ColumnC) VALUES (@ColumnA, @ColumnB, @ColumnC)" [/TD]

[TD="bgcolor: #F7F7F7"] command.Parameters.Add( New OleDbParameter( "@ColumnA" , OleDbType.VarWChar)) [/TD]

[TD="bgcolor: #F7F7F7"] command.Parameters.Add( New OleDbParameter( "@ColumnC" , OleDbType.VarWChar)) [/TD]

[TD="bgcolor: #F7F7F7"] connection.Open()[/TD]

[TD="bgcolor: #F7F7F7"] command.Transaction = transaction[/TD]

[TD="bgcolor: #F7F7F7"] Try [/TD]

[TD="bgcolor: #F7F7F7"] For i As Integer = 0 To DataGridView2.Rows.Count - 2 [/TD]

[TD="bgcolor: #F7F7F7"] command.Parameters( "@ColumnB" ).Value = DataGridView2.Rows(i).Cells(1).FormattedValue [/TD]

[TD="bgcolor: #F7F7F7"] command.ExecuteNonQuery()[/TD]

[TD="bgcolor: #F7F7F7"][/TD]

[TD="bgcolor: #F7F7F7"] Catch ex As Exception [/TD]

[TD="bgcolor: #F7F7F7"] Try [/TD]

[TD="bgcolor: #F7F7F7"][/TD]

[TD="bgcolor: #F7F7F7"] MessageBox.Show(rollBackEx.Message)[/TD]

[TD="bgcolor: #F7F7F7"][/TD]

[TD="bgcolor: #F7F7F7"][/TD]

[TD="bgcolor: #F7F7F7"][/TD]


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
 
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

Hi. I think it would be better to put that logic in the VB code and only go to the DB when it's a go. So, I'd do it like this. Can you work out the actual VB code?

For i AsInteger = 0 To DataGridView2.Rows.Count - 2
    'If column 4 is checked Then
        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() 
    'End If
Next i 
 
yes it right :) it must be like these

For i As Integer = 0 To DataGridView1.Rows.Count - 2
If DataGridView1.Rows(i).Cells(3).Value = True Then
command.Parameters("@ColumnA").Value = DataGridView1.Rows(i).Cells(0).FormattedValue
command.Parameters("@ColumnB").Value = DataGridView1.Rows(i).Cells(1).FormattedValue
command.Parameters("@ColumnC").Value = DataGridView1.Rows(i).Cells(2).FormattedValue
command.ExecuteNonQuery()
End If
Next i

thank you
 
Back
Top