Update dataviewgrid DB

Antrac1t

Member
Joined
Apr 28, 2014
Messages
13
Programming Experience
Beginner
Hi All,
I have code where I load data from database into dataviewgrid, but have problem with update. I tried to replace select for update but without any changes. Any ideas?

VB.NET:
Public Class Form1
    Public connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|datadirectory|\line_balance.accdb;persist security info = false"
    Public conn As New OleDbConnection



 Private Sub load_times_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_times.Click
Dim TblTime As String = "SELECT * FROM TblTime"        Dim Sqlcommand As New OleDbCommand
        Dim SqlAdapter As New OleDbDataAdapter
        Dim TABLE As New DataTable


        With Sqlcommand
            .CommandText = TblTime
            .Connection = conn
        End With


        With SqlAdapter
            .SelectCommand = Sqlcommand
            .Fill(TABLE)
        End With


        For i = 0 To TABLE.Rows.Count - 1
            With group_table
                .Rows.Add(TABLE.Rows(i)("Group"), TABLE.Rows(i)("Task"), TABLE.Rows(i)("Time"), TABLE.Rows(i)("TimeDesc"), TABLE.Rows(i)("Station"), TABLE.Rows(i)("Multiple"))
            End With
        Next i
End Sub
End Sub
 
You should generally use the same data adapter to save changes as you used to retrieve data. You call Fill to retrieve data, which executes the SelectCommand. To save changes, you call Update, which will execute the InsertCommand, UpdateCommand and DeleteCommand as needed. You need to either populate those properties yourself or else use a command builder to do it for you. Check out my thread below for an example of each:

Retrieving and Saving Data in Databases
 
I change code for this, but have error "Syntax error in UPDATE statement.". I tried to find how to fix it on google but without success

Private Sub update_times_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles update_times.Click
conn.Open()
For i = 0 To 10


Dim TABLE As New DataTable
Dim SqlCommand As New OleDbCommand
Dim SqlAdapter As New OleDbDataAdapter
Dim SqlQuery As String = "UPDATE TblTime SET Group = '" & group_table.Rows(i).Cells(0).Value & "'"
With SqlCommand
.CommandText = SqlQuery
.Connection = conn
.ExecuteNonQuery()
End With


Next i
End Sub
 
Back
Top