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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If Iwant to check if dataset1.SelectQuery1.column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an error.
 
Hi Dave,

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

Cor
 
I tried the statement: dataset1.SelectQuery.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:line 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:line 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(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd,
Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(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(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(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.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(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.ExecuteAsAssembly()
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

--
L. A. Jones


Cor Ligthert said:
Hi Dave,

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

Cor


Dave said:
If Iwant to check if dataset1.SelectQuery1.column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.

 
I tried the statement: dataset1.SelectQuery.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.column1))
 
if (dataset1.SelectQuery.column1 == null) // there is a null value


DbNull.Value doesn't equal null... The above line will generate an error if
column1 contains DbNull
 
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 said:
if (dataset1.SelectQuery.column1 == null) // there is a null value


DbNull.Value doesn't equal null... The above line will generate an error
if column1 contains DbNull
 
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 said:
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 said:
if (dataset1.SelectQuery.column1 == null) // there is a null value


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

 
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 said:
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 said:
if (dataset1.SelectQuery.column1 == null) // there is a null value


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

 
Hi,

Dave said:
If Iwant to check if dataset1.SelectQuery1.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.Iscolumn1Null() )
{
}


HTH,
Greetings

 
Hi Bart,

Thank very much, it worked!
--
L. A. Jones


Bart Mermuys said:
Hi,

Dave said:
If Iwant to check if dataset1.SelectQuery1.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.Iscolumn1Null() )
{
}


HTH,
Greetings
 
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/en-us/library/swwa298f.aspx

I hope this helps,

Cor


Dave said:
Dear Cor Ligthert,

You mentioned: "...if a row in the database exist with a null value (whatshould 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 said:
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 said:
if (dataset1.SelectQuery.column1 == null) // there is a null value

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

 
Hi Dave,

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/d...n-us/createdb/cm_8_des_02_92k3.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.
 
Hi..
you can check the column value with DBNull like this..

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

Regards,
Gopal
 
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 said:
Hi..
you can check the column value with DBNull like this..

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

Regards,
Gopal
If Iwant to check if dataset1.SelectQuery1.column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.

 
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 said:
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 said:
Hi..
you can check the column value with DBNull like this..

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

Regards,
Gopal
If Iwant to check if dataset1.SelectQuery1.column1 ==
System.DBNull.Value. How do I do this? What I wrote above will give an
error.


 
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...?
 
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...
 
Mark Rae said:
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)
 
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
 

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

Back
Top