473,382 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Handling DBNull from databases

When checking for NULL values from a database, normally I would use

If Not row("NetSales") Is DBNull.Value Then

NetSales = row("NetSales")

End If

However, if I use a typed dataset then I can't check for NULL the same
way

If Not row.NetSales Is DBNull.Value Then

NetSales = row.NetSales

End If

Is there a better way to check for Null values when using typed
datasets?

Jun 13 '06 #1
6 5918
Charlie,

AFAIK is the typed dataset from version 1.1 replacing the dbnull values for
the normal zero value of the type of value. I don't know it in version 2.0
therefore what version are you using.

Cor

"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
When checking for NULL values from a database, normally I would use

If Not row("NetSales") Is DBNull.Value Then

NetSales = row("NetSales")

End If

However, if I use a typed dataset then I can't check for NULL the same
way

If Not row.NetSales Is DBNull.Value Then

NetSales = row.NetSales

End If

Is there a better way to check for Null values when using typed
datasets?

Jun 13 '06 #2
I am using version 1.1

Regardless of datatype I need to check for NULL values before
attempting to assign them to a something else. In a typed dataset
values can be NULL when pulled from a database, I would like to
leverage benefits of typed datasets in my code.

If I use a typed dataset and assign an object a value from that dataset
that is null it will throw an exception

Dim sngSales as Single = row.Sales

I need to check for a NULL value in the Sales column. I can do this by
referecing the column like this just fine.

If Not row("Sales") is dbNull.Value Then
Dim snSales as Single = row.Sales
End If

But it seems to defeat the benefit of a typed dataset in code. You
can't have values in a dataset default to their 'normal' zero values,
since 0 and NULL in a financial application are two different things.
Cor Ligthert [MVP] wrote:
Charlie,

AFAIK is the typed dataset from version 1.1 replacing the dbnull values for
the normal zero value of the type of value. I don't know it in version 2.0
therefore what version are you using.

Cor

"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
When checking for NULL values from a database, normally I would use

If Not row("NetSales") Is DBNull.Value Then

NetSales = row("NetSales")

End If

However, if I use a typed dataset then I can't check for NULL the same
way

If Not row.NetSales Is DBNull.Value Then

NetSales = row.NetSales

End If

Is there a better way to check for Null values when using typed
datasets?


Jun 13 '06 #3
Charlie,

I can do it for you but better is to do it yourself.

If you go to the solution explorer. Click on the button in top show all
files, than you can expand the dataset and see the vb code.

Just searching on things as dbnull in that will show you how it is handled.

Cor

"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@g10g2000cwb.googlegr oups.com...
I am using version 1.1

Regardless of datatype I need to check for NULL values before
attempting to assign them to a something else. In a typed dataset
values can be NULL when pulled from a database, I would like to
leverage benefits of typed datasets in my code.

If I use a typed dataset and assign an object a value from that dataset
that is null it will throw an exception

Dim sngSales as Single = row.Sales

I need to check for a NULL value in the Sales column. I can do this by
referecing the column like this just fine.

If Not row("Sales") is dbNull.Value Then
Dim snSales as Single = row.Sales
End If

But it seems to defeat the benefit of a typed dataset in code. You
can't have values in a dataset default to their 'normal' zero values,
since 0 and NULL in a financial application are two different things.
Cor Ligthert [MVP] wrote:
Charlie,

AFAIK is the typed dataset from version 1.1 replacing the dbnull values
for
the normal zero value of the type of value. I don't know it in version
2.0
therefore what version are you using.

Cor

"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
> When checking for NULL values from a database, normally I would use
>
> If Not row("NetSales") Is DBNull.Value Then
>
> NetSales = row("NetSales")
>
> End If
>
> However, if I use a typed dataset then I can't check for NULL the same
> way
>
> If Not row.NetSales Is DBNull.Value Then
>
> NetSales = row.NetSales
>
> End If
>
> Is there a better way to check for Null values when using typed
> datasets?
>

Jun 13 '06 #4
Although I appreciate the advice on how to use my designer, I have
found the answer on my own. For anyone looking, when using typed
datasets and checking for DBNULL's the proper way to do so is this...

Dim row as TypedDataset.TypedDatasetRow =
TypedDataset1.Tables(0).Row(0)

Referencing the dataset row this way will allow you to leverage type
checking at design time and will create functions belonging to the row
object that return a boolean if the data in a column is null.

If you have a column named NetSales in your dataset, then the dataset
will create an IsNetSalesNull function that returns a boolean value.
So the proper way to check for Null values would be the following

If Not row.IsNetSalesNull Then
Dim sngNetSales as Single = row.NetSales
End If

Cor Ligthert [MVP] wrote:
Charlie,

I can do it for you but better is to do it yourself.

If you go to the solution explorer. Click on the button in top show all
files, than you can expand the dataset and see the vb code.

Just searching on things as dbnull in that will show you how it is handled.

Cor

"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@g10g2000cwb.googlegr oups.com...
I am using version 1.1

Regardless of datatype I need to check for NULL values before
attempting to assign them to a something else. In a typed dataset
values can be NULL when pulled from a database, I would like to
leverage benefits of typed datasets in my code.

If I use a typed dataset and assign an object a value from that dataset
that is null it will throw an exception

Dim sngSales as Single = row.Sales

I need to check for a NULL value in the Sales column. I can do this by
referecing the column like this just fine.

If Not row("Sales") is dbNull.Value Then
Dim snSales as Single = row.Sales
End If

But it seems to defeat the benefit of a typed dataset in code. You
can't have values in a dataset default to their 'normal' zero values,
since 0 and NULL in a financial application are two different things.
Cor Ligthert [MVP] wrote:
Charlie,

AFAIK is the typed dataset from version 1.1 replacing the dbnull values
for
the normal zero value of the type of value. I don't know it in version
2.0
therefore what version are you using.

Cor

"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
> When checking for NULL values from a database, normally I would use
>
> If Not row("NetSales") Is DBNull.Value Then
>
> NetSales = row("NetSales")
>
> End If
>
> However, if I use a typed dataset then I can't check for NULL the same
> way
>
> If Not row.NetSales Is DBNull.Value Then
>
> NetSales = row.NetSales
>
> End If
>
> Is there a better way to check for Null values when using typed
> datasets?
>


Jun 13 '06 #5
Charlie,

I don't think that this is a good advice. If you had followed what I told
you had find something as this in one minute.
\\\
Public Property City As String
Get
Try
Return CType(Me(Me.tableEmployees.CityColumn),String)
Catch e As InvalidCastException
Throw New StrongTypingException("Cannot get value
because it is DBNull.", e)
End Try
End Get
Set
Me(Me.tableEmployees.CityColumn) = value
End Set
End Property
///

With otherwords catching this exception in a try block when you use this
property will give you the needed result in a strongly typed way.

I am at the moment only active with version 2005, and that while as most of
the regular contributers to dotnet newsgroups do I not like the strongly
dataset versions from before 2005. I knew that it was something like this
but did not know anymore exactly how.

However, because I don't want that other have the wrong answer as the
propper way, have I taken some time for this. But that was more easy to do
for your if you had taken my advice, I had no project to test this.

As I see it well, than you are now just going around the strongly typed
dataset by using the non typed part of that.

Cor
"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@g10g2000cwb.googlegr oups.com...
Although I appreciate the advice on how to use my designer, I have
found the answer on my own. For anyone looking, when using typed
datasets and checking for DBNULL's the proper way to do so is this...

Dim row as TypedDataset.TypedDatasetRow =
TypedDataset1.Tables(0).Row(0)

Referencing the dataset row this way will allow you to leverage type
checking at design time and will create functions belonging to the row
object that return a boolean if the data in a column is null.

If you have a column named NetSales in your dataset, then the dataset
will create an IsNetSalesNull function that returns a boolean value.
So the proper way to check for Null values would be the following

If Not row.IsNetSalesNull Then
Dim sngNetSales as Single = row.NetSales
End If

Cor Ligthert [MVP] wrote:
Charlie,

I can do it for you but better is to do it yourself.

If you go to the solution explorer. Click on the button in top show all
files, than you can expand the dataset and see the vb code.

Just searching on things as dbnull in that will show you how it is
handled.

Cor

"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@g10g2000cwb.googlegr oups.com...
>I am using version 1.1
>
> Regardless of datatype I need to check for NULL values before
> attempting to assign them to a something else. In a typed dataset
> values can be NULL when pulled from a database, I would like to
> leverage benefits of typed datasets in my code.
>
> If I use a typed dataset and assign an object a value from that dataset
> that is null it will throw an exception
>
> Dim sngSales as Single = row.Sales
>
> I need to check for a NULL value in the Sales column. I can do this by
> referecing the column like this just fine.
>
> If Not row("Sales") is dbNull.Value Then
> Dim snSales as Single = row.Sales
> End If
>
> But it seems to defeat the benefit of a typed dataset in code. You
> can't have values in a dataset default to their 'normal' zero values,
> since 0 and NULL in a financial application are two different things.
>
>
> Cor Ligthert [MVP] wrote:
>> Charlie,
>>
>> AFAIK is the typed dataset from version 1.1 replacing the dbnull
>> values
>> for
>> the normal zero value of the type of value. I don't know it in version
>> 2.0
>> therefore what version are you using.
>>
>> Cor
>>
>> "Charlie Brown" <cb****@duclaw.com> schreef in bericht
>> news:11**********************@f6g2000cwb.googlegro ups.com...
>> > When checking for NULL values from a database, normally I would use
>> >
>> > If Not row("NetSales") Is DBNull.Value Then
>> >
>> > NetSales = row("NetSales")
>> >
>> > End If
>> >
>> > However, if I use a typed dataset then I can't check for NULL the
>> > same
>> > way
>> >
>> > If Not row.NetSales Is DBNull.Value Then
>> >
>> > NetSales = row.NetSales
>> >
>> > End If
>> >
>> > Is there a better way to check for Null values when using typed
>> > datasets?
>> >
>

Jun 13 '06 #6
I am not going around the strongly typed dataset at all. .net creates
those functions to test for NULL values by itself whenever you create a
strong dataset. The reason for using strong datasets is compile time
type checking, intellisense, and compact code. I can use the Try catch
method as well, but why not check for NULL myself when I know it will
be there. I am using the strong type dataset to its full potential. It
PROVIDES a way to check for NULL values for a reason. If it didn't
provide a way, then a Try Catch block would be more correct.
Cor Ligthert [MVP] wrote:
Charlie,

I don't think that this is a good advice. If you had followed what I told
you had find something as this in one minute.
\\\
Public Property City As String
Get
Try
Return CType(Me(Me.tableEmployees.CityColumn),String)
Catch e As InvalidCastException
Throw New StrongTypingException("Cannot get value
because it is DBNull.", e)
End Try
End Get
Set
Me(Me.tableEmployees.CityColumn) = value
End Set
End Property
///

With otherwords catching this exception in a try block when you use this
property will give you the needed result in a strongly typed way.

I am at the moment only active with version 2005, and that while as most of
the regular contributers to dotnet newsgroups do I not like the strongly
dataset versions from before 2005. I knew that it was something like this
but did not know anymore exactly how.

However, because I don't want that other have the wrong answer as the
propper way, have I taken some time for this. But that was more easy to do
for your if you had taken my advice, I had no project to test this.

As I see it well, than you are now just going around the strongly typed
dataset by using the non typed part of that.

Cor
"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@g10g2000cwb.googlegr oups.com...
Although I appreciate the advice on how to use my designer, I have
found the answer on my own. For anyone looking, when using typed
datasets and checking for DBNULL's the proper way to do so is this...

Dim row as TypedDataset.TypedDatasetRow =
TypedDataset1.Tables(0).Row(0)

Referencing the dataset row this way will allow you to leverage type
checking at design time and will create functions belonging to the row
object that return a boolean if the data in a column is null.

If you have a column named NetSales in your dataset, then the dataset
will create an IsNetSalesNull function that returns a boolean value.
So the proper way to check for Null values would be the following

If Not row.IsNetSalesNull Then
Dim sngNetSales as Single = row.NetSales
End If

Cor Ligthert [MVP] wrote:
Charlie,

I can do it for you but better is to do it yourself.

If you go to the solution explorer. Click on the button in top show all
files, than you can expand the dataset and see the vb code.

Just searching on things as dbnull in that will show you how it is
handled.

Cor

"Charlie Brown" <cb****@duclaw.com> schreef in bericht
news:11**********************@g10g2000cwb.googlegr oups.com...
>I am using version 1.1
>
> Regardless of datatype I need to check for NULL values before
> attempting to assign them to a something else. In a typed dataset
> values can be NULL when pulled from a database, I would like to
> leverage benefits of typed datasets in my code.
>
> If I use a typed dataset and assign an object a value from that dataset
> that is null it will throw an exception
>
> Dim sngSales as Single = row.Sales
>
> I need to check for a NULL value in the Sales column. I can do this by
> referecing the column like this just fine.
>
> If Not row("Sales") is dbNull.Value Then
> Dim snSales as Single = row.Sales
> End If
>
> But it seems to defeat the benefit of a typed dataset in code. You
> can't have values in a dataset default to their 'normal' zero values,
> since 0 and NULL in a financial application are two different things.
>
>
> Cor Ligthert [MVP] wrote:
>> Charlie,
>>
>> AFAIK is the typed dataset from version 1.1 replacing the dbnull
>> values
>> for
>> the normal zero value of the type of value. I don't know it in version
>> 2.0
>> therefore what version are you using.
>>
>> Cor
>>
>> "Charlie Brown" <cb****@duclaw.com> schreef in bericht
>> news:11**********************@f6g2000cwb.googlegro ups.com...
>> > When checking for NULL values from a database, normally I would use
>> >
>> > If Not row("NetSales") Is DBNull.Value Then
>> >
>> > NetSales = row("NetSales")
>> >
>> > End If
>> >
>> > However, if I use a typed dataset then I can't check for NULL the
>> > same
>> > way
>> >
>> > If Not row.NetSales Is DBNull.Value Then
>> >
>> > NetSales = row.NetSales
>> >
>> > End If
>> >
>> > Is there a better way to check for Null values when using typed
>> > datasets?
>> >
>


Jun 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brendan McLoughlin | last post by:
Hi, I am looking for opinions and alternatives for handling null values in a data object which reads a record from a database table. This object will have properties which will be populated...
5
by: Mark | last post by:
Assume that "some_app_setting" does not exist in my web.config file. Then, the first line of code below assigns the value null to strApplication. Why doesn't my IF statement return true, and...
9
by: Raul M. Colon | last post by:
Hi! Im developing a web application that reads from a database and populate a form with a DataReader. When the reader reach a null value in the database, the application throws an exception. In...
2
by: jack | last post by:
hi all im facing a problem in error handelling. what im doing is getting the images from the database to my csharp application form .. now here in some of the records contains images along with...
1
by: DKode | last post by:
I find myself writing repetitive functions for handling null values from my DB like so: Private Function SetDateNull(ByVal p_date As Object) As Date If (TypeOf (p_date) Is System.DBNull) Then...
2
by: Oleg Ogurok | last post by:
Hi all, I have a DatePicker ASP.NET control that allows users to enter date (e.g. a calendar control). The control has the following property public DateTime Value { get; set; } I've placed...
6
by: tshad | last post by:
I have a value coming from my Object (or it could also be from a SqlDbReader) where I need to test for DBNull and 0. I tried to do it in one call: if (not (newPosition.ReportsTo is...
8
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, if (DataReader.ToString() != String.Empty) { oCategory.ParentId = Convert.ToInt32(DataReader); } ParentId has some null values coming from my database. Is this the correct way to...
1
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm writing a demo website in VB/Dot Net 2.0. The database is SQL 2000 and the design is at least partially legacy. (And this is the first real web work I've done in 2.0. All previous work was...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.