StrongTypingException = DBNULL problem
I have a strongly typed dataset which I assign its value to my variables. There are fields in the data which I allow null (leave blank). I keep getting a StrongTypingException because of DBNULL value. Is there away for me to code it so that to say "yes it is ok to have some value to be null?"
I have a lot of variables which I need to set value to from the dataset so to write try and catch for each of my variables could be very messy but if there is no other way then that is what I have to do. Thank you for anyone help.
Re: StrongTypingException = DBNULL problem
Each column in a typed DataTable is a property whose type matches the data type of the column, e.g. String, Integer, etc. As such you can never get from, or assign to, one of those properties a null value because DBNull.Value can never match the property type. To work around this, database columns that allow null are mapped to properties that have two helper methods. If the property is named "Something" then you also have a SetSomethingNull method and an IsSomethingNull method, which will assign null to the corresponding field and tell you if the corresponding field is null respectively.
Re: StrongTypingException = DBNULL problem
Thank you for your help again. So if I have 10 variables to set value to from the dataset would I have to check that each one to see if it is not NULL as follow (Which I would have to repeat for all 10 variables)?
If Not DataSet1.table1(BindingSource1.Position).IsNameNull Then
x=DataSet1.table1(BindingSource1.Position).Name
end if
OR can I write one statement to check all 10 values for NULL something like this:
if not isSOMETHINGNULL then
var1=DataSet1.table1(BindingSource1.Position).Name
var2=DataSet1.table1(BindingSource1.Position).Address
.
.
.
.
var10=DataSet1.table1(BindingSource1.Position).City
Re: StrongTypingException = DBNULL problem
You would have to test each property that CAN be null individually. Of course, you wouldn't use DataSet1.Table1 and BindingSource1.Postition over and over. You'd get the DataRow object first and use that:
vb.net Code:
Dim row As Table1DataRow = DataSet1.Table1(BindingSource1.Position)
If Not row.IsNameNull() Then
var1 = row.Name
End If
If Not row.IsAddressNull() Then
var2 = row.Address
End If
Re: StrongTypingException = DBNULL problem
is "table1" in "table1DataRow" the name of my table? if so it does not seem to be there.
Re: StrongTypingException = DBNULL problem
How would I know what your tables are called? It's an EXAMPLE. The whole point of typed DataSets is that all the objects are typed to YOUR data, so the names used depend on YOUR data. Use whatever names are appropriate for YOUR data.
Re: StrongTypingException = DBNULL problem
I know that it is an EXAMPLE. My table name is Company (table1=Company) so what I am saying is that I don't see "CompanyDataRow".
Re: StrongTypingException = DBNULL problem
I see. What is the type of your table's Item property? That's the type of the rows.