Solved

handling nullable datatypes when using typed dataset

Posted on 2007-03-30
6
406 Views
Last Modified: 2008-02-01
I have a typed DataSet as my Data Access Layer.

In my BLL, I have the following static method:

   public static Support GetJob(int Id)
    {
        SupportDAL.SupportRequestDataTable dt = sAdapter.GetJob(Id);
        Support s = new Support();

        if (dt.Count > 0)
        {
            SupportDAL.SupportRequestRow r = dt[0];

            s.AssignedTo = r.IsAssignedToNull() ? <what goes here>: Convert.ToInt16(r.AssignedTo);
           // and more stuff goes here
        }
        return s;
    }

s.AssignedTo is of type short and is nullable.
Since my database has null values for "AssignedTo", I need to use IsAssignedToNull(). I am confused on what I should assign to s.AssignedTo if the value is in fact null in the database. see the <what goes here> section in my code.

Please advise.
0
Comment
Question by:IUAATech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 18826102
alot of times for numerics, if I have (and need) nulls in the db, but it comes back to code into a numeric variable, I'll re-assign to -1
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 18826112
...of course using negative one doesn't work if negative one is a valid value for the field,
but I find that using -1 works well with foreign keys and bits in the db...
0
 

Author Comment

by:IUAATech
ID: 18826121
since I have indicated that "AssignedTo" is nullable, why can't I do something like:
s.AssignedTo = r.IsAssignedToNull() ? null : Convert.ToInt16(r.AssignedTo);

and what do you do for datetime?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:IUAATech
ID: 18826143
A thought:

can I do something like:
s.AssignedTo = r.IsAssignedToNull() ? s.AssignedTo : Convert.ToInt16(r.AssignedTo);

This compiles and seems to work since s.AssignedTo is null to start with.
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 250 total points
ID: 18826195
if it works for your needs, then go with it...it depends on how the rest of your BL and DL are written...
but when I saw the Convert.ToInt16, I thought that s.AssignedTo is an integer type.
in which case, s.AssignedTo is probably taking on a value of zero and not null....
0
 

Author Comment

by:IUAATech
ID: 18826209
ah ok. I get it.

Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question