VB.Net - DataGridView update LastUpdated field in SQL Table

Jimbo99999
Jimbo99999 used Ask the Experts™
on
Good Day Experts!

I am shopping for assitance with a new task in my project that I do not understand hot to achieve it in VB.net.  I have a DataGridView that presents the User with SQL table data.  The User updates the data ans hits the <Update> button and the updates are written back to the SQL table.

My new task is in that SQL table I need to update the [LAST UPDATED] field when a change is made to a record.  

So far I have gone down this path:
    1)Put [LAST UPDATED] in my DataGridView so it can be in the dataTable that gets pulled
       down from db so I can send changes to it back to the database
    2)So, now have to figure out how to get the current date/time stamp into the
       DataGridView Last Update column when changes to the record in the DataGridView
       is made?
    3)When I run the UpdateCommand then it gets updated back to the database?

Am I on the right track? If, so what do I do next?

Thanks,
jimbo99999
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Do you use adapter to update? Does user only change one row? Or multiple rows? You can use a trigger on the table though its not the preferred approach.

Author

Commented:
Hello

Yes I am using the adapter and they can change multiple row in the grid at a time.
Find the current row you are updating, usually from datagridview index changed event
Then update the data into the datarow, create a SqlCommandBuilder.
Simple example for you, if you already have the dataset and datatable properly setup and
bind to the datagridview, you should be able to understand the codes.

You can update the timestamp into the datarow instead into the datagridview.
Don't really understand what you meant by "[LAST UPDATED] field " so cannot advise.
If not mistaken, it should be a new column in your SQL Table for datetime, if that's so,
in the datarow, just   CurDataRow("Last_Updated") = Now

  Private Sub eUpdate()
        Dim CmdBuilder As SqlCommandBuilder
        Try
            'Update the current row
            Dim CurDataRow As DataRow = eTable.Rows(eCurRowID)
            CurDataRow("Category_Code") = txtCategoryCode.Text

            CmdBuilder = New SqlCommandBuilder(SQLDataAdapter1)
            SQLDataAdapter1.Update(Dataset1, "TableName")
            CmdBuilder = Nothing
            MsgBox("Record updated successfully.", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox("Exception Error: " & ex.Message.ToString)
            CmdBuilder = Nothing
        End Try
    End Sub
Most Valuable Expert 2012
Top Expert 2014
Commented:
Also see if handling the dataadapter.RowUpdating event allows you to modify the command or add to it

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.rowupdating.aspx

Author

Commented:
Excellent...I was able to get it working with your suggestion.  Thanks for the helpful link as well.

jimbo99999

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial