Programming for Everybody: Visual Basic.Net: Insert Checked rows from DataGridView to Access Database with source code

Visual Basic.Net: Insert Checked rows from DataGridView to Access Database with source code

 




Imports System.Data.OleDb

Public Class Form14

    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\hobbies.accdb")


    Private Sub load_data()

        conn.Open()

        Dim cmd As New OleDbCommand("select id,name1,telephone from names1", conn)

        Dim da As New OleDbDataAdapter

        da.SelectCommand = cmd

        Dim table1 As New DataTable

        table1.Clear()

        da.Fill(table1)

        DataGridView1.DataSource = table1

        conn.Close()

        Dim checkboxcol As New DataGridViewCheckBoxColumn

        checkboxcol.Width = 40

        checkboxcol.Name = "checkboxcol"

        checkboxcol.HeaderText = ""

        DataGridView1.Columns.Insert(0, checkboxcol)

    End Sub

     

    Private Sub Form14_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        load_data()

    End Sub


    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        For Each row As DataGridViewRow In DataGridView1.Rows

            Dim select1 As Boolean = Convert.ToBoolean(row.Cells("checkboxcol").Value)

            If select1 Then

                conn.Open()

                Dim sql As String = " insert into telephones(Id,name1,telephone) values(@id,@name1,@telephone)"

                Dim cmd1 As New OleDbCommand(sql, conn)

                cmd1.Parameters.AddWithValue("@id", row.Cells("ID").Value)

                cmd1.Parameters.AddWithValue("@name1", row.Cells("name1").Value)

                cmd1.Parameters.AddWithValue("@telephone", row.Cells("telephone").Value)

                cmd1.ExecuteNonQuery()

                conn.Close()

            End If

        Next

        MessageBox.Show("Records Inserted")

    End Sub

No comments:

Post a Comment