469,327 Members | 1,277 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

How do I check to see if a dataset has a DBNull value

If Iwant to check if dataset1.SelectQuery1[i].column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an error.
--
L. A. Jones
Oct 28 '06 #1
19 18445

Hi Dave,

if (dataset1.SelectQuery[i].column1 == null) // there is a null value

Cor
"Dave" <Da**@discussions.microsoft.comschreef in bericht
news:25**********************************@microsof t.com...
If Iwant to check if dataset1.SelectQuery1[i].column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.
--
L. A. Jones

Oct 28 '06 #2
I tried the statement: dataset1.SelectQuery[i].column1 == null and I got the
error:
System.Data.StrongTypingException was unhandled
Message="The value for column 'TaxType' in table 'TaxSelect' is DBNull."
Source="Retail"
StackTrace:
at Retail.dsTaxRate.TaxSelectRow.get_TaxType() in C:\Documents and
Settings\HP_Owner\My Documents\Visual Studio
2005\Projects\Retail\Retail\dsTaxRate.Designer.cs: line 516
at Retail.frmPayrollSetUp.LoadComboBox(ComboBox cmb, String taxType)
in C:\Documents and Settings\HP_Owner\My Documents\Visual Studio
2005\Projects\Retail\Retail\frmPayrollSetUp.cs:lin e 83
at Retail.frmPayrollSetUp.frmPayrollSetUp_Load(Object sender,
EventArgs e) in C:\Documents and Settings\HP_Owner\My Documents\Visual Studio
2005\Projects\Retail\Retail\frmPayrollSetUp.cs:lin e 33
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow( HandleRef hWnd,
Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolea n value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at Retail.frmMain.frmMain_Load(Object sender, EventArgs e) in
C:\Documents and Settings\HP_Owner\My Documents\Visual Studio
2005\Projects\Retail\Retail\frmMain.cs:line 22
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessa ge(HandleRef
hWnd, Int32 msg, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Retail.Program.Main() in C:\Documents and Settings\HP_Owner\My
Documents\Visual Studio 2005\Projects\Retail\Retail\Program.cs:line 33
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAss embly()
at
System.Runtime.Hosting.ApplicationActivator.Create Instance(ActivationContext
activationContext, String[] activationCustomData)
at
System.Runtime.Hosting.ApplicationActivator.Create Instance(ActivationContext
activationContext)
at
Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

--
L. A. Jones
"Cor Ligthert [MVP]" wrote:
>
Hi Dave,

if (dataset1.SelectQuery[i].column1 == null) // there is a null value

Cor
"Dave" <Da**@discussions.microsoft.comschreef in bericht
news:25**********************************@microsof t.com...
If Iwant to check if dataset1.SelectQuery1[i].column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.
--
L. A. Jones


Oct 28 '06 #3
On Sat, 28 Oct 2006 06:54:01 -0700, Dave wrote:
I tried the statement: dataset1.SelectQuery[i].column1 == null and I got the
error:

System.Data.StrongTypingException was unhandled
Message="The value for column 'TaxType' in table 'TaxSelect' is DBNull."
Source="Retail"
StackTrace:
As is expected, you cannot compare DBNull to null, and DBNull.Value !=
null. What you can do is the following:

if (Convert.IsDBNull(dataset1.SelectQuery1[i].column1))
--
Tom Porterfield
Oct 28 '06 #4
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
if (dataset1.SelectQuery[i].column1 == null) // there is a null value
DbNull.Value doesn't equal null... The above line will generate an error if
column1 contains DbNull
Oct 28 '06 #5
Mark,
DbNull.Value doesn't equal null... The above line will generate an error
if column1 contains DbNull
In my idea can a column not contain a db.null value. There is just no value.
I was overreading that it was a strongly typed dataset, which will in most
types catch the DBNull.value themselves.

Which means that if a row in the database exist with a null value (what
should be impossible with a good one) while that should not, will thrown an
error.

Cor

"Mark Rae" <ma**@markNOSPAMrae.comschreef in bericht
news:%2******************@TK2MSFTNGP02.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
>if (dataset1.SelectQuery[i].column1 == null) // there is a null value

DbNull.Value doesn't equal null... The above line will generate an error
if column1 contains DbNull

Oct 28 '06 #6
Hi Cor,

DataTable only uses DBNull values, never null, hence the
DataColumn.AllowDBNull property but no "AllowNull" property.

Here's an example:

DataTable table = new DataTable();

// add an integer column
table.Columns.Add("col1", typeof(int));

// add a string column
table.Columns.Add("col2", typeof(string));

// add a row with explicit null values in both columns
table.Rows.Add(null, null);

Console.WriteLine(
"Int row is DBNull: " + ((table.Rows[0]["col1"] is DBNull)
? "true"
: "false"));

Console.WriteLine(
"String row is DBNull: " + ((table.Rows[0]["col2"] is DBNull)
? "true"
: "false"));
Console output:

Int row is DBNull: true
String row is DBNull: true

--
Dave Sexton

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:OA****************@TK2MSFTNGP03.phx.gbl...
Mark,
>DbNull.Value doesn't equal null... The above line will generate an error if
column1 contains DbNull

In my idea can a column not contain a db.null value. There is just no value.
I was overreading that it was a strongly typed dataset, which will in most
types catch the DBNull.value themselves.

Which means that if a row in the database exist with a null value (what
should be impossible with a good one) while that should not, will thrown an
error.

Cor

"Mark Rae" <ma**@markNOSPAMrae.comschreef in bericht
news:%2******************@TK2MSFTNGP02.phx.gbl...
>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl. ..
>>if (dataset1.SelectQuery[i].column1 == null) // there is a null value

DbNull.Value doesn't equal null... The above line will generate an error if
column1 contains DbNull


Oct 28 '06 #7
Dear Cor Ligthert,

You mentioned: "...if a row in the database exist with a null value (what >
should be impossible with a good one) while that should not...". My question
is: Suppose I have a table with columns which allow nulls, then how can I
prevent DBNulls from being placed in that column considering that I prefer
not place an empty string in the column. What effect does placing an empty
string in a column have on the database (specifically in terms of database
size) compared to if a column was set to DBNull?
Additionally, I have seen this thread address most the "why" issues. I
understand most of it. But could you show the "how" factor.
--
L. A. Jones
"Cor Ligthert [MVP]" wrote:
Mark,
DbNull.Value doesn't equal null... The above line will generate an error
if column1 contains DbNull

In my idea can a column not contain a db.null value. There is just no value.
I was overreading that it was a strongly typed dataset, which will in most
types catch the DBNull.value themselves.

Which means that if a row in the database exist with a null value (what
should be impossible with a good one) while that should not, will thrown an
error.

Cor

"Mark Rae" <ma**@markNOSPAMrae.comschreef in bericht
news:%2******************@TK2MSFTNGP02.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
if (dataset1.SelectQuery[i].column1 == null) // there is a null value
DbNull.Value doesn't equal null... The above line will generate an error
if column1 contains DbNull


Oct 28 '06 #8
Hi,

"Dave" <Da**@discussions.microsoft.comwrote in message
news:25**********************************@microsof t.com...
If Iwant to check if dataset1.SelectQuery1[i].column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.
If you use a typed DataSet, then you must use the Set???Null and Is???Null
functions (where ??? is the name of the column), eg. :

if ( dataset1.SelectQuery[i].Iscolumn1Null() )
{
}
HTH,
Greetings

--
L. A. Jones

Oct 28 '06 #9
Hi Bart,

Thank very much, it worked!
--
L. A. Jones
"Bart Mermuys" wrote:
Hi,

"Dave" <Da**@discussions.microsoft.comwrote in message
news:25**********************************@microsof t.com...
If Iwant to check if dataset1.SelectQuery1[i].column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.

If you use a typed DataSet, then you must use the Set???Null and Is???Null
functions (where ??? is the name of the column), eg. :

if ( dataset1.SelectQuery[i].Iscolumn1Null() )
{
}
HTH,
Greetings

--
L. A. Jones


Oct 28 '06 #10
Dave,

In my idea is the "best" method (whatever that is) for that using binding
and than the binding events.

http://windowssdk.msdn.microsoft.com.../swwa298f.aspx

I hope this helps,

Cor
"Dave" <Da**@discussions.microsoft.comschreef in bericht
news:31**********************************@microsof t.com...
Dear Cor Ligthert,

You mentioned: "...if a row in the database exist with a null value (what
>
should be impossible with a good one) while that should not...". My
question
is: Suppose I have a table with columns which allow nulls, then how can I
prevent DBNulls from being placed in that column considering that I prefer
not place an empty string in the column. What effect does placing an empty
string in a column have on the database (specifically in terms of database
size) compared to if a column was set to DBNull?
Additionally, I have seen this thread address most the "why" issues. I
understand most of it. But could you show the "how" factor.
--
L. A. Jones
"Cor Ligthert [MVP]" wrote:
>Mark,
DbNull.Value doesn't equal null... The above line will generate an
error
if column1 contains DbNull

In my idea can a column not contain a db.null value. There is just no
value.
I was overreading that it was a strongly typed dataset, which will in
most
types catch the DBNull.value themselves.

Which means that if a row in the database exist with a null value (what
should be impossible with a good one) while that should not, will thrown
an
error.

Cor

"Mark Rae" <ma**@markNOSPAMrae.comschreef in bericht
news:%2******************@TK2MSFTNGP02.phx.gbl. ..
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...

if (dataset1.SelectQuery[i].column1 == null) // there is a null value

DbNull.Value doesn't equal null... The above line will generate an
error
if column1 contains DbNull



Oct 29 '06 #11
Hi Dave,

<snip>
Suppose I have a table with columns which allow nulls, then how can I
prevent DBNulls from being placed in that column considering that I prefer
not place an empty string in the column
A DataColumn Typed as System.String can contain DBNull only when the
AllowDBNull property is true. No DataColumn can contain null (a null
reference, as in "no object to reference").

Try compiling the code I supplied in my previous post in this thread and
you'll see what I mean.
What effect does placing an empty
string in a column have on the database (specifically in terms of database
size) compared to if a column was set to DBNull?
Normalized tables usually don't allow null values in columns. If you require
the semantics of a null value, which means that you accept records with
"missing" data, then go ahead and allow null values in your database column,
otherwise don't. In Sql Server you don't normally have to factor in
performance or data size when making that decision. You should make the
decision based on the appropriateness of having nulls and adjust for
performance or size if it becomes an issue (although I highly doubt that
replacing nulls with empty strings, or visa-versa, will affect performance or
size substantially, if even at all).

Estimating the Size of a Table on MSDN:
http://msdn.microsoft.com/library/de...asp?frame=true
Additionally, I have seen this thread address most the "why" issues. I
understand most of it. But could you show the "how" factor.
I believe this question was answered already.

--
Dave Sexton
Oct 29 '06 #12
Hi..
you can check the column value with DBNull like this..

if(dataset1.SelectQuery1[i].column1.Equals(System.DBNull.Value))
{
// logic goes
}

Regards,
Gopal

Dave wrote:
If Iwant to check if dataset1.SelectQuery1[i].column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an error.
--
L. A. Jones
Oct 29 '06 #13
Hi Gopal,

It will work if you set NullValue to [Null] or [Empty] in the XSD designer,
but only if "column1" is Typed as System.String.

Any other Type for "column1" will always throw an exception when reading a
DBNull value. You can verify this by trying to change NullValue in the XSD
designer to anything other than [Throw Exception], for any column that is not
System.String.

--
Dave Sexton

"Gopal" <go**********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi..
you can check the column value with DBNull like this..

if(dataset1.SelectQuery1[i].column1.Equals(System.DBNull.Value))
{
// logic goes
}

Regards,
Gopal

Dave wrote:
>If Iwant to check if dataset1.SelectQuery1[i].column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.
--
L. A. Jones

Oct 29 '06 #14
Hi Gopal,

Bit of a correction;
It will work if you set NullValue to [Null] or [Empty] in the XSD designer,
but only if "column1" is Typed as System.String.
....But if you change NullValue to [Null] or [Empty], and "column1" is
System.String, then comparing to DBNull in your example won't work. You'd
have to then check for null or string.Empty, respectively.

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:ui****************@TK2MSFTNGP05.phx.gbl...
Hi Gopal,

It will work if you set NullValue to [Null] or [Empty] in the XSD designer,
but only if "column1" is Typed as System.String.

Any other Type for "column1" will always throw an exception when reading a
DBNull value. You can verify this by trying to change NullValue in the XSD
designer to anything other than [Throw Exception], for any column that is
not System.String.

--
Dave Sexton

"Gopal" <go**********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>Hi..
you can check the column value with DBNull like this..

if(dataset1.SelectQuery1[i].column1.Equals(System.DBNull.Value))
{
// logic goes
}

Regards,
Gopal

Dave wrote:
>>If Iwant to check if dataset1.SelectQuery1[i].column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.
--
L. A. Jones


Oct 29 '06 #15
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uD****************@TK2MSFTNGP04.phx.gbl...
In my idea is the "best" method (whatever that is) for that using binding
and than the binding events.
Does anyone have the slightest idea what this means...?
Oct 29 '06 #16
Mark,

A real sarcastic reply, however it shows only a lot about your knowledge.

Just one search on MSDN for binding events gives you this information.

http://windowssdk.msdn.microsoft.com.../ms613490.aspx

Cor

"Mark Rae" <ma**@markNOSPAMrae.comschreef in bericht
news:OB****************@TK2MSFTNGP03.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uD****************@TK2MSFTNGP04.phx.gbl...
>In my idea is the "best" method (whatever that is) for that using binding
and than the binding events.

Does anyone have the slightest idea what this means...?

Oct 30 '06 #17
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:O$**************@TK2MSFTNGP05.phx.gbl...
>In my idea is the "best" method (whatever that is) for that using binding
and than the binding events.
A real sarcastic reply, however it shows only a lot about your knowledge.
Not sarcastic at all. In fact, with my degree in foreign languages I can
usually just about decipher your posts, but the above had me stumped...
Oct 30 '06 #18

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:OP**************@TK2MSFTNGP04.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:O$**************@TK2MSFTNGP05.phx.gbl...
>>In my idea is the "best" method (whatever that is) for that using
binding and than the binding events.
>A real sarcastic reply, however it shows only a lot about your knowledge.

Not sarcastic at all. In fact, with my degree in foreign languages I can
usually just about decipher your posts, but the above had me stumped...
It would seem that a simple addition of "would be" and a "than" to "then"
substitution would have made it easy for someone with a degree in languages
to understand the post.

ie: In my idea is the "best" method (whatever that is) for that WOULD BE
using binding and THEN the binding events.

Maybe it's the comp sci in me -vs- the language degree in you, but it was
obvious to me at least. ;-)

--Gordon Smith (eMVP)
Oct 30 '06 #19
I see that you are proud that you can tell the world that you have degree in
foreign languages, but I use this contribution from the Americans to the
international expressions, to tell you what I find from your contribution to
this thread.

"yIy"

Cor

"Mark Rae" <ma**@markNOSPAMrae.comschreef in bericht
news:OP**************@TK2MSFTNGP04.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:O$**************@TK2MSFTNGP05.phx.gbl...
>>In my idea is the "best" method (whatever that is) for that using
binding and than the binding events.
>A real sarcastic reply, however it shows only a lot about your knowledge.

Not sarcastic at all. In fact, with my degree in foreign languages I can
usually just about decipher your posts, but the above had me stumped...

Oct 30 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Andrew Banks | last post: by
4 posts views Thread by jay | last post: by
1 post views Thread by WebBuilder451 | last post: by
reply views Thread by SAL | last post: by
2 posts views Thread by =?Utf-8?B?TWlrZSBPS0M=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.