Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I am currently experimenting with databinding.
Now there is one problem I cannot seem to fix.

I have a DataSet with a DataTable. In this DataTable I have a Column which does not allow null values.
Now I have a BindingSource which has the DataSet as its DataSource and the DataTable as its DataMember and a BindingNavigator which has the BindingSource as its BindingSource property. All goes well until I leave the non-null column empty and try to navigate to another row in the DataTable or add a new row. It makes the application crash altogether... If I just call the DataSource.EndEdit method I only get a NoNullAllowedException.

The fields of my DataTable are shown in TextBoxes, not DataGrids.

I have checked all the DataSource events, properties, subs and functions, but I cannot find any method that allows me to check if all values are OK before I update/add new row/ navigate to another row or call the DataSource.EndEdit method.

Any suggestions on overcoming this? Thanks.
Posted
Updated 28-Jan-11 11:32am
v2

1 solution

Amongst all the tags you applied to your question you forgot to include one for the type of Database (SQL Server, Access, MySQL, ????).

Still, one suggestion. If your database allows default values for columns setting that to 0 or any sensible number for your scenario might resolve the problem. If not, then having that TextBox initialized to a value of 0 (or whatever) might achieve the same result.

If neither of those suggestions suit, or as an alternative, consider handling the Validating[^] event.
 
Share this answer
 
v3
Comments
Sander Rossel 28-Jan-11 17:53pm    
I'm no fan of 'default values'. Either you enter a value or you don't. And if you don't, you just can't continue. The problem isn't in SQL. My DataTable is trying to add rows, which it can't do because of no-null contraints. Only if one or more rows are added to my DataTable the SQL Server comes into play, but by that time I hope to have valid DataRows :)
Sander Rossel 30-Jan-11 16:53pm    
Editing my previous post doesn't seem to work so...
How would it help to handle the Validating event?
Henry Minute 30-Jan-11 17:16pm    
During validation you check for a valid value. If there is not one, you can either force it to 0, for example, or raise a prompt to the user to input a valid response.
Sander Rossel 31-Jan-11 14:38pm    
What I do now is the following:

<pre>
Private Function ValidateValues() As Boolean
Dim view As DataRowView
Dim row As DataRow
Dim hasEmptyValues As Boolean

Me.Validate()

view = CType(bindingSource.Current, DataRowView)

row = view.Row

If row("Name").ToString = String.Empty Then
errorProvider.SetError(txtName, "Please specify a name.")
hasEmptyValues = True
Else
errorProvider.SetError(txtName, String.Empty)
End If

If row("Type").ToString = String.Empty Then
errorProvider.SetError(cbxType, "Please specify a valid type.")
hasEmptyValues = True
Else
errorProvider.SetError(cbxType, String.Empty)
End If

Return Not hasEmptyValues
End Function
</pre>

It works, but I have to check every column in my current DataSource row. I could also use the Validating event for each control or datagrid cell or whatever.
But is there no way to check every value in my current DataSource row directly? It knows which columns cannot be null and it knows all the values, so it should know if the current row is valid or not. I tried the row.HasErrors, but it returned no errors and crashed my application after that because of NULL values. I tried handling the DataSource events (it doesn't have a validating event), but it all comes down to the same thing, which is checking every value one by one. I don't specifically have a problem with that as long as I know there really is no other way...
Henry Minute 31-Jan-11 16:59pm    
Well, there is a DataGridView.RowValidating (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowvalidating.aspx) event that you might use. The example on that page shows using it together with the Cell.ErrorText property. If that would work any better, or how it would affect performance I have no idea as I have never used it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900