7 Replies - 3219 Views - Last Post: 20 October 2010 - 03:58 PM Rate Topic: -----

#1 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Typed DataSet - DBNull Handling

Posted 20 October 2010 - 12:13 PM

I was convinced to start using typed datasets by another forum member and untill now its made life a lot easier. Now ive come to testing some functions of my app I keep getting loads of exceptions.

In the dataset designer ive set the NullValue property to Empty for all of the strings but doesn't allow me to for datetime and number. Can anyone advise how I check if an int or datetime is = DBNull before trying to use it to avoid the exceptions?

Im mainly getting this problem when trying to set the values in text boxes and converting strings to int32s.

Thanks

Matt :angry2:

Is This A Good Question/Topic? 0
  • +

Replies To: Typed DataSet - DBNull Handling

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Typed DataSet - DBNull Handling

Posted 20 October 2010 - 12:33 PM

Have you tried to use Convert class?
if (Convert.IsDBNull(SomeObject))


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Typed DataSet - DBNull Handling

Posted 20 October 2010 - 12:46 PM

If it's a typed DataSet, you can check with row.IsMyFieldNull(). If it's just of type DataRow, with no type, then row.IsNull("MyField") should do it. You may also check with row["MyField"]==DBNull.Value.

DataSets will always throw errors for null. This is as it should be. You need to handle nulls properly if your table allows for them.
Was This Post Helpful? 2
  • +
  • -

#4 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: Typed DataSet - DBNull Handling

Posted 20 October 2010 - 12:58 PM

View Postbaavgai, on 20 October 2010 - 11:46 AM, said:

If it's a typed DataSet, you can check with row.IsMyFieldNull(). If it's just of type DataRow, with no type, then row.IsNull("MyField") should do it. You may also check with row["MyField"]==DBNull.Value.

DataSets will always throw errors for null. This is as it should be. You need to handle nulls properly if your table allows for them.


Thanks this solves my problem.

if (!NewClaimRow.IsCoverInceptionNull()) { MainFormInstance.TBInceptionDate.Text = NewClaimRow.CoverInception.ToShortDateString(); }

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Typed DataSet - DBNull Handling

Posted 20 October 2010 - 01:07 PM

For filling in text fields, you'll very often want to fill in a blank if the row value is null. I usually prefer this form for it:
this.TBInceptionDate.Text = row.IsCoverInceptionNull() ? "" : row.CoverInception.ToShortDateString();


Was This Post Helpful? 0
  • +
  • -

#6 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: Typed DataSet - DBNull Handling

Posted 20 October 2010 - 01:29 PM

View Postbaavgai, on 20 October 2010 - 12:07 PM, said:

For filling in text fields, you'll very often want to fill in a blank if the row value is null. I usually prefer this form for it:
this.TBInceptionDate.Text = row.IsCoverInceptionNull() ? "" : row.CoverInception.ToShortDateString();



Ok ive seen this sort of snippet around but havent used it because I didn't really understand it. Is it the case that if the result is false it used the value just after the question mark and if its true it uses the value after the :.

Thanks

P.S Is there any particular reason to just enter a blank if the value is null?
Was This Post Helpful? 0
  • +
  • -

#7 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: Typed DataSet - DBNull Handling

Posted 20 October 2010 - 02:20 PM

@Mouse88,

It's called the ternary operator. A lot of the most common programming languages have it.

Here is another post on D.I.C that shows an example on where/when to use it.

This post has been edited by keakTheGEEK: 20 October 2010 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Typed DataSet - DBNull Handling

Posted 20 October 2010 - 03:58 PM

View Postmouse88, on 20 October 2010 - 02:29 PM, said:

Is there any particular reason to just enter a blank if the value is null?


Yep. If there is already data in the text field, you'll want to blank it out if loading a new record and the value is null. There are a number of scenarios where this is likely to happen.

What you're doing is called "data binding", the manual way. I often do it the manual way. However, you should be aware that there are sophisticated built in methods for data binding. Visual Studio offers some automatic ways of hooking up data binding. More here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1