Monday, March 12, 2012

How to handle a integer null value in a (.xsd) dataset

Hi.

I want to pass a null value to an integer in a (stored) dataset. The column in the database allows null values, but the dataset throws an error. (VS2005)

Goos van Beek

If column in DataTable allows NULLs, you could assing value using DBNull.Value, not Null


Thanks for responding, Val.

How can I pass a DBNull value to the dataset? The user deletes an integer value in a bound textfield and the result is a frozen application.

Of course I can use unbound fields, which I usually do, but I have chosen for the easy way this time :-)

Goos van Beek.


In a case of binding, I do not think you have much control, but I believe application should not freeze in this case anyway. Do you get any exception? Do you trap any exceptons but do not handle them? In a case of unbound controls, you could assign value using next code, but you have to know index of the row that require the change

MyDataTable.Rows(IndexOfTheRowHere).Items("MyColumn)=DBNull.Value


Actually the application doesn't freeze, but the cursor can't leave the field until there is a integer value typed in the field. And that's the problem when the value should be null (or dbnull)

I know the record index, but i can't update a single value of that row.

this.MyDataSet.Tables[0].Rows[idx].Items["MyFieldName"] = DBNull.Value; doesn't work, because the there is no defenition for 'Items'

I can update the value in the underlaying database table, but not in the dataset.

Goos van Beek.


uhmm... (a little idea...i don't now how it fits your needs)...

you can set in design time a default value for your column...this value is never used by your application (e.g. invoice total value cannot be negative and you can use -1).

In runtime when the user clean the column you can set somthing like this

this.MyDataSet.Tables[0].Rows[idx].Items["MyFieldName"] = myDataset.MyTable.MyColumn.DefaultValue


Thanks for responding, Bob.

Sometimes you don't want a default value, although not in the userform.

I changed the datatype in the dataset from System.Int32 to System.String. This allows me to enter a null value in the field. The value in the table is updated by a SqlCommand. Only this is not what I want...

this.MyDataSet.Tables[0].Rows[idx].Items["MyFieldName"] = myDataset.MyTable.MyColumn.DefaultValue; doesn't work, because the there is no defenition for 'Items'

Isn't there a way to adapt the default behaviour of the dataset to handle null values for an integer?

Labels: , , , , , , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home