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.SupportRequestD ataTable dt = sAdapter.GetJob(Id);
Support s = new Support();
if (dt.Count > 0)
{
SupportDAL.SupportRequestR ow r = dt[0];
s.AssignedTo = r.IsAssignedToNull() ? <what goes here>: Convert.ToInt16(r.Assigned To);
// 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.
In my BLL, I have the following static method:
public static Support GetJob(int Id)
{
SupportDAL.SupportRequestD
Support s = new Support();
if (dt.Count > 0)
{
SupportDAL.SupportRequestR
s.AssignedTo = r.IsAssignedToNull() ? <what goes here>: Convert.ToInt16(r.Assigned
// 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.
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...
but I find that using -1 works well with foreign keys and bits in the db...
ASKER
since I have indicated that "AssignedTo" is nullable, why can't I do something like:
s.AssignedTo = r.IsAssignedToNull() ? null : Convert.ToInt16(r.Assigned To);
and what do you do for datetime?
s.AssignedTo = r.IsAssignedToNull() ? null : Convert.ToInt16(r.Assigned
and what do you do for datetime?
ASKER
A thought:
can I do something like:
s.AssignedTo = r.IsAssignedToNull() ? s.AssignedTo : Convert.ToInt16(r.Assigned To);
This compiles and seems to work since s.AssignedTo is null to start with.
can I do something like:
s.AssignedTo = r.IsAssignedToNull() ? s.AssignedTo : Convert.ToInt16(r.Assigned
This compiles and seems to work since s.AssignedTo is null to start with.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ah ok. I get it.
Thanks.
Thanks.