the first row value in datagridview is not updated in database using dataset

reedone816

Member
Joined
Mar 18, 2013
Messages
6
Programming Experience
Beginner
please help,
I'm using vb.net 2008 and mysql database. what is wrong with this code? because the change in first row on datagridview is not updated to database.

VB.NET:
    For i As Integer = 0 To DataGridView1.RowCount - 2
    DataGridView1.Rows(i).Cells(11).Value = "true"
    Next
    XTableAdapter.Update(XDataSet.Tables("Xtable"))
    XDataSet.AcceptChanges()

for example:
my data originally is like this:
row 1: "false"
row 2: "false"
row 3: "false"

then using the above code I change the value in datagridview:
row 1: "true"
row 2: "true"
row 3: "true"

but when I cross check it in the database, the first row is not updated:
row 1: "false"
row 2: "true"
row 3: "true"
 
Last edited:
You should pretty much never work with the grid in code like that. Presumably your DataTable is bound to your grid. You should bind it to a BindingSource and bind that to the grid. You then access the data via the BindingSource:
VB.NET:
For Each row As DataRowView In myBindingSource
    row(columnNameOrIndex) = someValue
Next
That will fix your issue as well as avoid various others.

Also, your call to AcceptChanges is pointless because Update calls AcceptChanges implicitly.
 
By the way, why are you using Strings containing "true" and "false"? You should be using actual Boolean values, i.e. True and False in VB and the appropriate data type in your database, which is BOOL or BOOLEAN for MySQL.
 
thanks for the fast reply :)
VB.NET:
For Each row As DataRowView In BindingSource1     
row(11) = "true"
Next 
XTableAdapter.Update(XDataSet.Tables("Xtable"))
is it like this? I've tried it but not working on the first row, the next row to last is updated.

for the value of writing "true"/"false", it is because the database belong to third party application "gammu", it's their default. I'm afraid to change the type if it will make the application unable to update the database...
 
I think I have to revert to old way, I just update it directly to the database...
VB.NET:
    mysqlcommand.commandtext
    mysqlcommand.ExecuteNonQuery()
    mytransaction.Commit()

So I guess it is solved then... :)
 
Last edited:
Back
Top