HELP: Updating data in a db

  • Thread starter Thread starter VB Programmer
  • Start date Start date
V

VB Programmer

Need help. Here's my example updating a db using an edited dataset. But,
for some reason the db is NOT being updated!

Private Sub UpdateData()
cnnTechNames.Open()

With daNetworkNames
.SelectCommand.CommandText = "SELECT * FROM NetworkNames WHERE
FullName = 'John Smith';"
.Fill(dsNetworkNames)
End With

With dsNetworkNames.Tables(0)
.Rows(0).BeginEdit()
' Increment privilege field by 1
.Rows(0)("Privilege") = CType(.Rows(0)("Privilege"), Short) + 1
.Rows(0).EndEdit()
.Rows(0).AcceptChanges()
End With

daNetworkNames.Update(dsNetworkNames)

cnnTechNames.Close()
End Sub

Please help! Also, I want to know the simplest way to update data in the
database.

Thanks.
 
You are calling AcceptChanges on your row. This changes its state from
Modified to Unchanged. Since it is now marked as Unchanged, there is
nothing to update.

Take that line out.
 
Simple. You can't update a database with just a dataset! Only a
DataAdapter can update a database. When you call
DataAdapter.Update(DataSet), internally the DataAdapter.UpdateCommand is
executed for each changed row. DataAdapter.DeleteCommand is executed for
each deleted row. DataAdapter.InsertCommand is called for each new row. If
one of the needed commands has not been defined on the DataAdapter, you will
get an exception.

The question is what is the best way to create your CRUD commands (Create,
Read, Update, Delete)?
1) you can use a CommandBuilder to autogenerate each command object and
assign them to the matching properties on the DataAdapter
2) you can create the commands manually
a) have them call stored procedures on the DB
b) execute some commandtext you define.
in either case it is best to create Parameter objects for the command.
 
Back
Top