Can anyone explain why a try catch block will not catch a DBNull, I had a try catch block round some code that updates some data in sql database and returns either 1 or 0.
But when testing last night it appears that some of the data I get from a remote source had some data missing, which made the update fail due to an if exists in sql and the return value was then null.
So i have added:
var result = cmd.Parameters["@new_identity"].Value;
DBNull.Value is not the same as null. So if you are testing for whether something is null, anything that is DBNull.Value will not be.
In addition, null in the context of a try-catch block belongs to C#. Database applications have their own NULL value. The meaning of NULL in SQL Server may not necessarily be the same as that defined by Oracle or PostGreSQL, for example (although
in practice, they generally mean the same thing). However, C# or indeed any other application language cannot assume that they are the same as their version of null. So the .NET framework has a special value for fields that contain a database NULL, and that
is DBNull.Value.
Simply, because DBNull and try/catch have nothing to do with each other.
try/catch lets your code intercept and handle an exception.
DBNull is not an exception. As already mentioned it is a value. You could put your question a different, but equivalent, way.
"Can anyone explain why a try catch block will not catch a number 3"
Seems absurd, right?
try/catch blocks deal with exceptions, DBNull is not an exception. Nor, it seems, does the code that you have written throw an exception when there is no data. Updates can fail, it would not normally be seen as exceptional behaviour.
CareerChange
Member
43 Points
180 Posts
DBNull theory lesson
Jan 03, 2013 10:53 AM|LINK
Hi
Can anyone explain why a try catch block will not catch a DBNull, I had a try catch block round some code that updates some data in sql database and returns either 1 or 0.
But when testing last night it appears that some of the data I get from a remote source had some data missing, which made the update fail due to an if exists in sql and the return value was then null.
So i have added:
var result = cmd.Parameters["@new_identity"].Value;
string message = (result == DBNull.Value) ? "0" : result.ToString();
Which catches the null value, I have also changed code in database and xml file to handle any null/empty values.
But as I was asking, why does the try catch block not catch dbnulls.
A quick theory lesson on this would be helpfull
Thanks
George
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: DBNull theory lesson
Jan 03, 2013 11:09 AM|LINK
DBNull.Value is not the same as null. So if you are testing for whether something is null, anything that is DBNull.Value will not be.
In addition, null in the context of a try-catch block belongs to C#. Database applications have their own NULL value. The meaning of NULL in SQL Server may not necessarily be the same as that defined by Oracle or PostGreSQL, for example (although in practice, they generally mean the same thing). However, C# or indeed any other application language cannot assume that they are the same as their version of null. So the .NET framework has a special value for fields that contain a database NULL, and that is DBNull.Value.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Paul Linton
Star
13403 Points
2531 Posts
Re: DBNull theory lesson
Jan 03, 2013 09:02 PM|LINK
Simply, because DBNull and try/catch have nothing to do with each other.
try/catch lets your code intercept and handle an exception.
DBNull is not an exception. As already mentioned it is a value. You could put your question a different, but equivalent, way.
"Can anyone explain why a try catch block will not catch a number 3"
Seems absurd, right?
try/catch blocks deal with exceptions, DBNull is not an exception. Nor, it seems, does the code that you have written throw an exception when there is no data. Updates can fail, it would not normally be seen as exceptional behaviour.
CareerChange
Member
43 Points
180 Posts
Re: DBNull theory lesson
Jan 03, 2013 10:18 PM|LINK
Hi Paul and Mike
Thanks for the replies, I understand what you mean and it clears it up in my own head now.
Seems the more books I read and the more questions I ask, the less I seem to know
Hopefully you can answer another query I have later when I post it.
Thanks
George