Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42329

VS 2010 Save Binded DGV

$
0
0
Hi,

I am beginner in visual studio.

I am having a DGV binded with data table (MS Access 2007) in that, user enters the Unit of Measurement and those data to be saved in the same DGV and I tried the following code to insert user typed data but it is also inserting already binded data (already in the table).

Please advice...

Code:

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

    Dim Cnn_UnitMaster As New OleDbConnection ' connection object to database
    Dim Conn_String1 As String = ("Provider=Microsoft.ACE.OLEDB.12.0;" & _
                      "Data Source=" & System.Environment.CurrentDirectory.ToString() & "\DataBase.accdb;" & _
                      "Jet OLEDB:Database Password=SR;")
    Cnn_UnitMaster.ConnectionString = Conn_String1
    Dim SQL_Cust1 As String = "INSERT INTO UnitMaster (UnitName) VALUES (@UnitName);"
    Cnn_UnitMaster.Open()
    Dim Cmd_Insert1 As New OleDbCommand(SQL_Cust1, Cnn_UnitMaster)
    With Me.DataGridView1
        For i As Integer = 0 To .RowCount - 1
            UnitName = .Rows(i).Cells(1).Value
            With Cmd_Insert1
                If Not String.IsNullOrEmpty(UnitName) OrElse _
                UnitName <> "" OrElse UnitName <> Nothing Then
                    .Parameters.AddWithValue("@UnitName", UnitName)
                    .ExecuteNonQuery()
                    .Parameters.Clear()
                End If

            End With
        Next
        Cmd_Insert1.Dispose()
        Cnn_UnitMaster.Close()
    End With
End Sub


Viewing all articles
Browse latest Browse all 42329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>