Solved

handling nullable datatypes when using typed dataset

Posted on 2007-03-30
6
403 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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