Testing for DBNulls

  • Thread starter Thread starter Fred Chateau
  • Start date Start date
F

Fred Chateau

I can't seem to find a test for DBNulls. Whatever I try doesn't work.

for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.
 
Fred,

Have you set a point in the debugger and looked at what the value of:

dataSet.Identity[x].Chain_ID

Actually is?
 
Fred said:
I can't seem to find a test for DBNulls. Whatever I try doesn't work.

for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.

What kind of object does your variable "dataSet" reference?

If you have a DataRow, it has the method IsNull that you can use to
check for DbNull values.

If you have a data reader, it has the method IsDbNull that you can use
to check for DbNull values.
 
Well, I can't believe I found it but I did. I had to edit the code generated
by XSD.

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public string Chain_ID {
get {
try {
if (!Convert.IsDBNull(this[this.tableIdentity.Chain_IDColumn])) <--
Added code
return ((string) (this[this.tableIdentity.Chain_IDColumn]));
else
return "0";
<-- Added code
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException("The value for column
\'Chain_ID\' in table \'Identity\' is DBNull.", e);
}
}
set {
this[this.tableIdentity.Chain_IDColumn] = value;
}
}

--
Regards,

Fred Chateau
http://hotelmotelnow.com


Nicholas Paldino said:
Fred,

Have you set a point in the debugger and looked at what the value of:

dataSet.Identity[x].Chain_ID

Actually is?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
I can't seem to find a test for DBNulls. Whatever I try doesn't work.

for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.

--
Regards,

Fred Chateau
http://hotelmotelnow.com
 
Fred,

Well, I wouldn't do that. What I was recommending was to determine what
the value that was being returned was through the debugger in your program,
and then using that knowledge to change the code.

Generally, I wouldn't change the designer code, because it will not
respect your changes if you make changes through the designer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
Well, I can't believe I found it but I did. I had to edit the code
generated by XSD.

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public string Chain_ID {
get {
try {
if (!Convert.IsDBNull(this[this.tableIdentity.Chain_IDColumn])) <--
Added code
return ((string) (this[this.tableIdentity.Chain_IDColumn]));
else
return "0"; <-- Added code
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException("The value for column
\'Chain_ID\' in table \'Identity\' is DBNull.", e);
}
}
set {
this[this.tableIdentity.Chain_IDColumn] = value;
}
}

--
Regards,

Fred Chateau
http://hotelmotelnow.com


Nicholas Paldino said:
Fred,

Have you set a point in the debugger and looked at what the value of:

dataSet.Identity[x].Chain_ID

Actually is?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
I can't seem to find a test for DBNulls. Whatever I try doesn't work.

for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.

--
Regards,

Fred Chateau
http://hotelmotelnow.com
 
I'm not sure how I could do anything else. It tries to cast the XML Node
value to a string and if the cast fails, it throws an Invalid Cast
Exception, then sends out the message: The value for column <columnName> in
table <tableName> is DBNull.

It should pass the DBNull along so I can test for it, but it never gets that
far. You can't modify your code when you don't even have access to the
variable.

Do you think there's a way to catch the exception? Even if there is, isn't
that bad practice to use an exception to determine a decision branch in the
code?

--
Regards,

Fred Chateau
http://hotelmotelnow.com


--
Regards,

Fred Chateau
http://hotelmotelnow.com

Nicholas Paldino said:
Fred,

Well, I wouldn't do that. What I was recommending was to determine
what the value that was being returned was through the debugger in your
program, and then using that knowledge to change the code.

Generally, I wouldn't change the designer code, because it will not
respect your changes if you make changes through the designer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
Well, I can't believe I found it but I did. I had to edit the code
generated by XSD.

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public string Chain_ID {
get {
try {
if (!Convert.IsDBNull(this[this.tableIdentity.Chain_IDColumn])) <--
Added code
return ((string) (this[this.tableIdentity.Chain_IDColumn]));
else
return "0"; <-- Added code
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException("The value for column
\'Chain_ID\' in table \'Identity\' is DBNull.", e);
}
}
set {
this[this.tableIdentity.Chain_IDColumn] = value;
}
}

--
Regards,

Fred Chateau
http://hotelmotelnow.com


Nicholas Paldino said:
Fred,

Have you set a point in the debugger and looked at what the value of:

dataSet.Identity[x].Chain_ID

Actually is?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I can't seem to find a test for DBNulls. Whatever I try doesn't work.

for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.

--
Regards,

Fred Chateau
http://hotelmotelnow.com
 
Fred,

This is a typed data set, right? You should be able to highlight the
column in the designer and indicate that you want it to return DBNull when
null is encountered for that column.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
I'm not sure how I could do anything else. It tries to cast the XML Node
value to a string and if the cast fails, it throws an Invalid Cast
Exception, then sends out the message: The value for column <columnName>
in table <tableName> is DBNull.

It should pass the DBNull along so I can test for it, but it never gets
that far. You can't modify your code when you don't even have access to
the variable.

Do you think there's a way to catch the exception? Even if there is, isn't
that bad practice to use an exception to determine a decision branch in
the code?

--
Regards,

Fred Chateau
http://hotelmotelnow.com


--
Regards,

Fred Chateau
http://hotelmotelnow.com

Nicholas Paldino said:
Fred,

Well, I wouldn't do that. What I was recommending was to determine
what the value that was being returned was through the debugger in your
program, and then using that knowledge to change the code.

Generally, I wouldn't change the designer code, because it will not
respect your changes if you make changes through the designer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
Well, I can't believe I found it but I did. I had to edit the code
generated by XSD.

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public string Chain_ID {
get {
try {
if (!Convert.IsDBNull(this[this.tableIdentity.Chain_IDColumn])) <--
Added code
return ((string) (this[this.tableIdentity.Chain_IDColumn]));
else
return "0"; <-- Added code
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException("The value for column
\'Chain_ID\' in table \'Identity\' is DBNull.", e);
}
}
set {
this[this.tableIdentity.Chain_IDColumn] = value;
}
}

--
Regards,

Fred Chateau
http://hotelmotelnow.com


in message Fred,

Have you set a point in the debugger and looked at what the value
of:

dataSet.Identity[x].Chain_ID

Actually is?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I can't seem to find a test for DBNulls. Whatever I try doesn't work.

for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.

--
Regards,

Fred Chateau
http://hotelmotelnow.com
 
for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.

foreach (IdentityRow identtyRow in dataSet.Identity)
{

if (identityRow.IsChain_IDNull)
{
}
else
{
}

dataSet.POI_Entity.New
}
 
Well, that worked too.

The choices are empty, null and throw exception. I set the value to return
an empty string and then tested for the empty string.

Thanks again, Nicholas.

--
Regards,

Fred Chateau
http://hotelmotelnow.com

Nicholas Paldino said:
Fred,

This is a typed data set, right? You should be able to highlight the
column in the designer and indicate that you want it to return DBNull when
null is encountered for that column.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
I'm not sure how I could do anything else. It tries to cast the XML Node
value to a string and if the cast fails, it throws an Invalid Cast
Exception, then sends out the message: The value for column <columnName>
in table <tableName> is DBNull.

It should pass the DBNull along so I can test for it, but it never gets
that far. You can't modify your code when you don't even have access to
the variable.

Do you think there's a way to catch the exception? Even if there is,
isn't that bad practice to use an exception to determine a decision
branch in the code?

--
Regards,

Fred Chateau
http://hotelmotelnow.com


--
Regards,

Fred Chateau
http://hotelmotelnow.com

Nicholas Paldino said:
Fred,

Well, I wouldn't do that. What I was recommending was to determine
what the value that was being returned was through the debugger in your
program, and then using that knowledge to change the code.

Generally, I wouldn't change the designer code, because it will not
respect your changes if you make changes through the designer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Well, I can't believe I found it but I did. I had to edit the code
generated by XSD.

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public string Chain_ID {
get {
try {
if (!Convert.IsDBNull(this[this.tableIdentity.Chain_IDColumn])) <--
Added code
return ((string) (this[this.tableIdentity.Chain_IDColumn]));
else
return "0"; <-- Added code
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException("The value for column
\'Chain_ID\' in table \'Identity\' is DBNull.", e);
}
}
set {
this[this.tableIdentity.Chain_IDColumn] = value;
}
}

--
Regards,

Fred Chateau
http://hotelmotelnow.com


"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message Fred,

Have you set a point in the debugger and looked at what the value
of:

dataSet.Identity[x].Chain_ID

Actually is?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I can't seem to find a test for DBNulls. Whatever I try doesn't work.

for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.

--
Regards,

Fred Chateau
http://hotelmotelnow.com
 
Fred,

Just a note, in this case, I would opt to return null, since there is a
difference between null and an empty string in both .NET and DB-land.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
Well, that worked too.

The choices are empty, null and throw exception. I set the value to return
an empty string and then tested for the empty string.

Thanks again, Nicholas.

--
Regards,

Fred Chateau
http://hotelmotelnow.com

Nicholas Paldino said:
Fred,

This is a typed data set, right? You should be able to highlight the
column in the designer and indicate that you want it to return DBNull
when null is encountered for that column.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Chateau said:
I'm not sure how I could do anything else. It tries to cast the XML Node
value to a string and if the cast fails, it throws an Invalid Cast
Exception, then sends out the message: The value for column <columnName>
in table <tableName> is DBNull.

It should pass the DBNull along so I can test for it, but it never gets
that far. You can't modify your code when you don't even have access to
the variable.

Do you think there's a way to catch the exception? Even if there is,
isn't that bad practice to use an exception to determine a decision
branch in the code?

--
Regards,

Fred Chateau
http://hotelmotelnow.com


--
Regards,

Fred Chateau
http://hotelmotelnow.com

in message Fred,

Well, I wouldn't do that. What I was recommending was to determine
what the value that was being returned was through the debugger in your
program, and then using that knowledge to change the code.

Generally, I wouldn't change the designer code, because it will not
respect your changes if you make changes through the designer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Well, I can't believe I found it but I did. I had to edit the code
generated by XSD.

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public string Chain_ID {
get {
try {
if (!Convert.IsDBNull(this[this.tableIdentity.Chain_IDColumn]))
<-- Added code
return ((string) (this[this.tableIdentity.Chain_IDColumn]));
else
return "0"; <-- Added code
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException("The value for column
\'Chain_ID\' in table \'Identity\' is DBNull.", e);
}
}
set {
this[this.tableIdentity.Chain_IDColumn] = value;
}
}

--
Regards,

Fred Chateau
http://hotelmotelnow.com


"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message Fred,

Have you set a point in the debugger and looked at what the value
of:

dataSet.Identity[x].Chain_ID

Actually is?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I can't seem to find a test for DBNulls. Whatever I try doesn't work.

for (int x = 0; x < dataSet.Identity.Rows.Count; x++)
{
DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();

if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value))
dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID;
else
dataRow["Chain_Id"] = 0;

dataSet.Tables["POI_Entity"].Rows.Add(dataRow);
}

I've also tried

(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)

That doesn't work either.

--
Regards,

Fred Chateau
http://hotelmotelnow.com
 
Well, that worked too.
The choices are empty, null and throw exception. I set the value to return
an empty string and then tested for the empty string.

Sorry, I sent my last email off prematurely (on another thread). For
strongly-typed datasets life is much easier. Take a look at the
"whatever.Designer.cs" file under your ".xsd" file. Here's a quickly-written
analogue of your original post. It's what you should be doing but it might
be a little rough since I don't have your actual file to work with (and I'm
doing this from memory).

foreach (IdentityRow identyRow in dataSet.Identity)
{
POI_EntityRow newRow = dataSet.POI_Entity.NewPOI_EntityRow();

if (!identityRow.IsChain_IDNull)
{
newRow.Chain_Id = idenityRow.Chain_ID;
}
else
{
newRow.Chain_Id = 0;
}

dataSet.POI_Entity.AddPOI_EntityRow(newRow);
}
 
Larry, I set the designer back to "Throw Exception" and used your method of
testing for DBNulls. It works well, and like Nicholas said, it's better to
handle the problem in your own code than changing code in designers, or even
designer settings.

I think this is the way DBNull was meant to be used, that is, testing for it
like you would test for object nulls and responding in an appropriate
manner.
 
Larry, I set the designer back to "Throw Exception" and used your method
of testing for DBNulls. It works well, and like Nicholas said, it's better
to handle the problem in your own code than changing code in designers, or
even designer settings.

I think this is the way DBNull was meant to be used, that is, testing for
it like you would test for object nulls and responding in an appropriate
manner.

Yes, but also note how much easier it is to work in general using
strongly-typed datasets. There's a class for each table and a corresponding
row class where each field (column) is exposed as a property IOW, you no
longer have to work with a "DataTable" or "DataRow" directly anymore (most
of the time anyway). Other properties/functions in "whatever.Designer.cs"
greatly simplify the entire "DataSet" experience. You can also easily extend
each table and row class by redeclaring it in "whatever.cs" using the
"partial" keyword (for VS2005). This allows you to add your own custom
functions which is the way I handle things. Good luck.
 

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