Sainty checks on new data input to a strongly typed dataset?

R

Roy Chastain

A contract programmer wrote a lot of code for us dealing with updating a database. He is gone and I am clueless.

I THINK we have a strongly typed dataset. What I have in VS is a ClientDataSet.xsd that has a tool (MSDataSetGenerator)
associated with it. The resulting .CS has lots of code. One example is below.

public int Count {
get {
try {
return ((int)(this[this.tableClientStationSpec.CountColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("Cannot get value because it is DBNull.", e);
}
}
set {
this[this.tableClientStationSpec.CountColumn] = value;
}
}

In the graphical XSD display, Count is simple marked as int with no restrictions as to value etc.

From what I can tell we have a DataGrid Control that is used to populate and edit the values in the database. The DataSource
property of the DataGrid is set at form design time to a instance of the DataSet class.
The DataGridshows 2 columns one being a name and the other being the Count previously discussed.

The problem I am having is that when I enter a new row via the DataGrid, if I fill in name only and move the cursor to the next
row the Count column displays (Empty) and when I try to update the database I get an Invalid Cast Exception from the cast that is
shown in the Count property code above.

I understand that a null, can not be considered an int at this point, but what I want to do is prevent any attempt to update the
database when the column is empty (and perhaps apply other sanity checks in the process).

Obviously I don't want to mess with the generated code shown above, so where do I look in the plumbing for a place to review the
data as it leaves the DataGrid and goes to the DataSet/database. In my mind we should not let the user leave the current row of
the DataGrid until all the columns are correct. The second choice would be to abort the 'Apply' operation if the data is invalid.

Please pardon me if some of this does not make sense to you. This is my first ever effort at database programming and needless to
say my first go at DataGrids and DataSets.

Thanks
 
M

Miha Markic [MVP C#]

Hi Roy,

One neat way would be to provide a default value for that column.
Or, in xsd designer, set Count MinOccurs to empty string (meaning that it
will be required value).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Roy Chastain said:
A contract programmer wrote a lot of code for us dealing with updating a
database. He is gone and I am clueless.
I THINK we have a strongly typed dataset. What I have in VS is a
ClientDataSet.xsd that has a tool (MSDataSetGenerator)
associated with it. The resulting .CS has lots of code. One example is below.

public int Count {
get {
try {
return ((int)(this[this.tableClientStationSpec.CountColumn]));
}
catch (InvalidCastException e) {
throw new StrongTypingException("Cannot get value because it is DBNull.", e);
}
}
set {
this[this.tableClientStationSpec.CountColumn] = value;
}
}

In the graphical XSD display, Count is simple marked as int with no restrictions as to value etc.

From what I can tell we have a DataGrid Control that is used to populate
and edit the values in the database. The DataSource
property of the DataGrid is set at form design time to a instance of the DataSet class.
The DataGridshows 2 columns one being a name and the other being the Count previously discussed.

The problem I am having is that when I enter a new row via the DataGrid,
if I fill in name only and move the cursor to the next
row the Count column displays (Empty) and when I try to update the
database I get an Invalid Cast Exception from the cast that is
shown in the Count property code above.

I understand that a null, can not be considered an int at this point, but
what I want to do is prevent any attempt to update the
database when the column is empty (and perhaps apply other sanity checks in the process).

Obviously I don't want to mess with the generated code shown above, so
where do I look in the plumbing for a place to review the
data as it leaves the DataGrid and goes to the DataSet/database. In my
mind we should not let the user leave the current row of
the DataGrid until all the columns are correct. The second choice would
be to abort the 'Apply' operation if the data is invalid.
Please pardon me if some of this does not make sense to you. This is my
first ever effort at database programming and needless to
 

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