Results 1 to 3 of 3

Thread: I need to modify the simple insert command code here

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2011
    Location
    saudi arabia
    Posts
    37

    Question I need to modify the simple insert command code here

    I need to modify the simple insert command code here

    Code:
        
        Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click  
                Dim connectionString As String = "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 As New OleDbConnection(connectionString)  
         
                    Dim cmdText As String = "INSERT INTO DGV (ColumnA, ColumnB, ColumnC) VALUES (@ColumnA, @ColumnB, @ColumnC)" 
                    Dim command As New 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 As Integer = 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)  
                        End Try 
         
                    End Try 
         
                End Using  
         
            End Sub
    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: I need to modify the simple insert command code here

    Pseudo code
    Code:
     For i As Integer = 0 To DataGridView2.Rows.Count - 2
         If DataGridViewCheckBoxColumn4.Checked = True 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
    You will need to work out the correct syntax for the IF statement, but this is what you need to do.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2011
    Location
    saudi arabia
    Posts
    37

    Re: I need to modify the simple insert command code here

    Quote Originally Posted by Hack View Post
    Pseudo code
    Code:
     For i As Integer = 0 To DataGridView2.Rows.Count - 2
         If DataGridViewCheckBoxColumn4.Checked = True 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
    You will need to work out the correct syntax for the IF statement, but this is what you need to do.
    thanx mr.Hack it is right whet little change

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width