Solved

handling nullable datatypes when using typed dataset

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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