Why won't my DataTable.Update?

M

Mike

Dear Group,

I have a .NET 2.0 .DLL assembly containing a dataset with three tables. I
reference this from a standalone .NET 2.0 EXE. I am writing a login form
which uses the single Dataset from the .NET 2.0 .DLL. One of the tables
(staff) contains a TableAdapter that fills using a single parameter
(@UserName). I am trying to code a simple login form for which I pass a
username and read the password. All is well and fine with this, however I
want to write back to the "lastlogon" column and update it with a timestamp.
I'm only using one table, so this shouldn't be too difficult?

The following code doesn't break at all, but the table never updates. What
am I doing wrong? I'm worried that I might be updating my own TableAdapter
in memory which is disconnected from the database, or my instance of my
TableAdapter isn't connected to the satellite .DLL assembly. The satellite
..DLL assembly is strongly named and the Dataset Strongly Typed. Any ideas?
Thanks in advance for any help!

Dim StaffDT As Staff.STAFFDataTable
Dim StaffTA As StaffTableAdapters.STAFFTableAdapter

StaffDT = New Staff.STAFFDataTable

StaffTA = New StaffTableAdapters.STAFFTableAdapter

StaffTA.FillByUserName(StaffDT, UsernameTextBox.Text)

' Found this username
If StaffDT.Rows.Count = 1 Then

' Check password
If StaffDT.Rows(0).Item("password") = Trim(PasswordTextBox.Text) Then

' Mark this login attempt
StaffDT.Rows(0).Item("lastlogon") = Now()

' Accept Changes
StaffDT.AcceptChanges()

' Commit Changes
StaffTA.Update(StaffDT)

' Show Switchboard (successful login)
ShowSwitchboard()

Else
MsgBox("Incorrect Password", MsgBoxStyle.Exclamation,
My.Application.Info.AssemblyName)

End If

Else

MsgBox("This username cannot be found in the database. Please ask your
system administrator to create a username for you", _
MsgBoxStyle.Exclamation, My.Application.Info.AssemblyName)

End If
 
M

Mike

Mike said:
' Accept Changes
StaffDT.AcceptChanges()

If I remove this line, everything works. Any idea why?

Also if anyone's reading this and knows how - where can I find the Data Form
Wizard? It's not available on my development environment.

Warm Regards,

Mike
 
C

Cor Ligthert [MVP]

M

Mike

Cor Ligthert said:
Mike,


AcceptChanges is an abbreviation for

Accept changes in the dataset/datatable as if all updates are done.
(One of the most misleading methods names for newbies and than they forget
in never more).

Oh yes, that was confusing! Thanks for the explanation.
To push the changes from a control to a datasource you have to use the
EndCurrentEdit

http://msdn.microsoft.com/library/d...indingmanagerbaseclassendcurrentedittopic.asp

I have a BindingNavigator rather than a BindingManager, yet when I call the
..EndEdit() method on another form, nothing happens - I've tried checking SQL
Profiler and no SQL statements go through to the database. Any ideas?

Warm Regards,

Mike
 
M

Mike

Cor Ligthert said:
Mike

The endedit does as far as I have seen nothing,

If this is a problem try than that endcurrenedit

In this sample you can see it used at the end

http://www.vb-tips.com/default.aspx?ID=0bcf3aa6-0da7-4a6e-9061-ac8b53818af6

I hope this helps,

I can't use EndCurrentEdit as it is not a method available in either the
DataSet, BindingSource or BindingNavigator objects.
BindingContext(DirectCast(DataGridView1.DataSource,
DataView)).EndCurrentEdit() <

Mike
 
C

Cor Ligthert [MVP]

Mike said:
I can't use EndCurrentEdit as it is not a method available in either the
DataSet, BindingSource or BindingNavigator objects.
Probably,

BindingContext(StaffDT).EndCurrentEdit()

Or show otherwise the code where you bind your data to the controls.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top