Trap errors on Datatable

Hi,

I have a datatable and i have made some columns on the datatable mandatory. I have bound the datatable to a grid from infugistics. Is there anyway of trapping errors from the datatable when a columns value is not supplied....I need to supply a more meaningful error message.

Is there any event on a datatable that i can manipulate

Thanks

Anwar
anwarmirAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arif_eqbalCommented:
Yes check out the RowChanging And/Or ColumnChanging Event

eg.

AddHandler DataTable.RowChanging, AddressOf DataTable_RowChanging

then

    Private Sub DataTable_RowChanging(ByVal sender As System.Object, ByVal e As DataRowChangeEventArgs)
        If e.Row.Item(0) <> "Correct Vlaue" Then
            Throw (New Exception("Column Value Not Proper"))
        End If
    End Sub
0
anwarmirAuthor Commented:
Just a few points:

1) Where Do I  declare the ADD Handler part

AddHandler DataTable.RowChanging, AddressOf DataTable_RowChanging

2) What is the AddressofDatatable part

3) I Guess this will be fired automatically. But how how can i distinguish between errors like mandatory rows and unique rows ?

 

0
gajender_99Commented:
creat a function fro check mandatery columns and call it before updating if it passes update the table or raise an exception
first count total rows and for each row check for the mandatory column



0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

anwarmirAuthor Commented:
How does that fall in to the datatable events. Or is this a new solution ?
0
gajender_99Commented:
on the form do something like this

function checkmend()

return true/false
end function


when closing/updating before calling the updatedatsset call me.checkmend
depending on the status call updatedataset
0
anwarmirAuthor Commented:
sorry but i'm new to VB.NET what i want to do is the trap the error message genertated when a column is not supplied. There must be an event where this can be trapped. I dont c why i have to write a function to check if a column is manadatory.
0
arif_eqbalCommented:
OK Let me explain it a bit

You have a dataGrid where you are displaying data, Now this dataGrid would be having a Datatable as its DataSource

Now any operation on the data in the dataGrid is actually an operatin on the underlying dataTable, this means if you are changing a value in the datagrid it is actually being changed in the dataTable which is behind thata dataGrid. So when we want to check if a paricular Column value is supplied or not or if supplied is it proper or not we need to trap the events of the underlying dataTable.

Now if you are familiar with good old VB6 we used to Have Dim WithEvents Obj as Class in order to have events of a particular class. This method is still available in .NET so we can trap the events of the DataTable using With Events, However since we need only one event we can use the alternate method of AddHandler. This AddHandler adds an event Handler which is automatically called when an event occurs.

Now where to Add the code for AddHandler, you should write it just after you initialize your DataTable. say on Form Load or wherever you are initializing the DataTable

Now the AddHandler takes 2 arguments one the Event second the Pointer to the function the will be called on tha event so

AddHandler DataTable.RowChanging, AddressOf DataTable_RowChanging

(AddressOf means paas the address of the function i.e. function pointer)

Now when you write this the event will be fired Now you can code that event to do whatever checking you want. (Also you can use DataTable.ColumnChanging this will be fired for each column, however Rowchangin is good enough you can validate after one full row has been added or modified)

now the event Function

 Private Sub DataTable_RowChanging(ByVal sender As System.Object, ByVal e As DataRowChangeEventArgs)

'Here e.Row contains the dataRow that is being modified/Added so do the validation here
       
        If e.Row.Item(0)="" Then
                 'If Column 0 value is empty then give a meaningful Error, or messagebox
                 Throw (New Exception("Employee ID Not Provided"))
        ElseIf Not IsNumeric(e.Row.Item(0)) Then
            'Column value provided but not proper
            Throw (New Exception("Column Value Not Proper"))
        End If
    End Sub

There are a lot of options inside this Procedure the argument e provides a lot of Info. For eg. in case of an Edit operation you can check what was the old value what is the new value etc. Look for the documentation you might get a lot of information


0
anwarmirAuthor Commented:
Thanks  for this....I decided to use the column changing event.Although the error is trapped after i display the msgbox if i click on OK the data still remains. How can i get it to cancel the current data that was input for the column and highlight the cell for new data: This is what I have right now:

Private Sub dtExchangerates_ColumnChanging(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs) Handles dtExchangerates.ColumnChanging

 If e.Column.ColumnName = "fExchangeRate" Then
If CType(e.ProposedValue, Integer) <= 0 Then
'Display message to user that the showcode must be entered
MessageBox.Show(goCaptions.GetMessage(EXCHANGE_RATE_GREATER_THAN_ZERO))

 
grdCurrency.ActiveCell = grdCurrency.ActiveRow.Cells(1)
grdCurrency.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.ActivateCell)
grdCurrency.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode)
End If
End If

    End Sub
0
arif_eqbalCommented:
use the event handler to ColumnChanged rather than ColumnChanging

You can not revert back to the original value from inside this(ColumnChanging) event handler, directly (There are ways to do even that but not what microsoft wants you to do)
Conceptually the data has not yet been changed in the datatable (Changing fires before the change is made) so where to go back (It would have been good if there was something like e.Cancel=True and that would have reverted to old value)

However if you use the ColumnChanged Event handler you can use e.Row.RejectChanges to get back the earlier values

All your code will remain same...

Private Sub dtExchangerates_ColumnChanged(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs) Handles dtExchangerates.ColumnChanged  '-----> ColumnCHANGED

 If e.Column.ColumnName = "fExchangeRate" Then
If CType(e.ProposedValue, Integer) <= 0 Then
'Display message to user that the showcode must be entered
MessageBox.Show(goCaptions.GetMessage(EXCHANGE_RATE_GREATER_THAN_ZERO))
'-----------> Add this line
e.Row.rejectChanges
 
grdCurrency.ActiveCell = grdCurrency.ActiveRow.Cells(1)
grdCurrency.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.ActivateCell)
grdCurrency.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode)
End If
End If

    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anwarmirAuthor Commented:
Thanks thats cleared up alot of confusion
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.