how do i refere to the specific records/columns in a vb.net form

Signo.X

Well-known member
Joined
Aug 21, 2006
Messages
76
Location
Australia
Programming Experience
1-3
i have a vb.net form that adds/deleteds/edit etc.. record from an Access DB..

i need to log every operation the user does providing the specific names of the data in the record that has been modified.

for e.g. the user enters in the form 2 record :
22 john sports and
23 smith T.V.

then the user navigates through the form, after the user is done, he clicks on save button on the bindingnavigator to update the DB with the new values ... and then the user opens the log file to see what records has been added/modified .. he see the follwing in the log file :
22 john sports and
23 smith T.V.
------ has been added .

how do i refere to the specific records/columns that has been added/modified??

Thanks in Advance ..

~Signo.X :confused:
 
The best way to do this would be to wait until the user is about to save the data and then test each changed row. You can then record whether the row was added, edited or deleted and which columns were edited. You use the RowState to determine the type of change made and then you use the Item property to get the current and/or original value of each field.
VB.NET:
Dim changes As DataTable = myDataTable.GetChanges()

If changes IsNot Nothing Then
    For Each row As DataRow In changes.Rows
        Select Case row.RowState
            Case DataRowState.Added
                'Get the current value of the ID for the new row.
                MessageBox.Show("Added: ID = " & row("ID").ToString())
            Case DataRowState.Modified
                For Each col As DataColumn In changes.Columns
                    Dim colName As String = col.ColumnName
                    Dim originalValue As String = row(colName, DataRowVersion.Original).ToString()
                    Dim currentValue As String = row(colName).ToString()

                    If currentValue <> originalValue Then
                        MessageBox.Show(String.Format("Modified: ID = {0}; Original {1} = {2}; Current {1} = {3}", _
                                                      row("ID"), _
                                                      colName, _
                                                      originalValue, _
                                                      currentValue))
                    End If
                Next
            Case DataRowState.Deleted
                'Get the original value of the ID for the deleted row.
                MessageBox.Show("Deleted: ID = " & row("ID", DataRowVersion.Original).ToString())
        End Select
    Next row
End If
 
Thanks Heaps!
finally you've answered the question using code :D - are you in a good mood or some thing today :)
any way, that works except for one problem .. when i try to modify a column..then save , i get this exception :

"index zero based must be greater than or equal to zero and less than the size of argument list"

its some thing to do with that :

MessageBox.Show(String.Format("Modified: ID = {0}; Original {1} = {2}; Current {1} = {3}",row"ID"), colName,originalValue, currentValue))

any idea why im getting this exception and how tofix it ?
also , im not sure exactely what does the above line does with the args {0} etc... ???

Thanks again!!
~signo.X
 
You need to take another look at my code, your code and where we've each placed our parentheses. I specifically format my code to show where the parameters belong. 'colName', 'originalValue' and 'currentValue' are all parameters to the String.Format method in my code, hence the indenting, and you've made them parameters of MessageBox.Show. If you don't know how String.Format works then MSDN is waiting. It's basically just a straight replacement of parameters by position. You're telling String.Format to expect four parameters by specifying 0 to 3 and you're only passing it one parameter.
 
Back
Top