Solved

handling nullable datatypes when using typed dataset

Posted on 2007-03-30
6
405 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
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!

 

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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