Link to home
Start Free TrialLog in
Avatar of IUAATech
IUAATech

asked on

handling nullable datatypes when using typed dataset

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.
Avatar of samtran0331
samtran0331
Flag of United States of America image

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
...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...
Avatar of IUAATech
IUAATech

ASKER

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?
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.
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ah ok. I get it.

Thanks.