What is the best way to check data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,
I call a stored procedure that returns a bunch of output parameters. And all
of those are nullable data. Is this the best approach to validating the
value? I don't want the value checked using IF and I am not using
SqlDataTypes as well.

itemId = parms[1].Value != null ||
!parms[1].Value.ToString().Equals(string.Empty) ? (int)parms[1].Value : 0;
itemGroupGuid = parms[2].Value != null ||
!parms[2].Value.ToString().Equals(Guid.Empty) ? (Guid)parms[2].Value :
Guid.Empty;
 
Amil said:
Hi all,
I call a stored procedure that returns a bunch of output parameters. And all
of those are nullable data. Is this the best approach to validating the
value? I don't want the value checked using IF and I am not using
SqlDataTypes as well.

itemId = parms[1].Value != null ||
!parms[1].Value.ToString().Equals(string.Empty) ? (int)parms[1].Value : 0;
itemGroupGuid = parms[2].Value != null ||
!parms[2].Value.ToString().Equals(Guid.Empty) ? (Guid)parms[2].Value :
Guid.Empty;

You need to define what values your proc will be able to return.
1. An int parameter could not hold an empty string so you do not need to
check for that.

2. Why would your guid param be guid.empty?
It should be either null or a valid guid

So

itemId = (parms[1].value != dbnull ? (int)parms[1].value : 0;
etc..


JB
 
I wrote static methods to do this, one for each type, and use them:

itemGroupGuid = DataHandler.DBValueToGuid(parms[2], 0);

the first argument is the value from the DB; the second is the default
to use if the DB value is DBNull. Another example:

int itemId = DataHandler.DBValueToInt(parms[1], 0);

etc. At the very least, this makes sure that once your compare-to-null
logic is tested once (for each type), you can't mess it up anywhere
else. That alone has no doubt saved me hours of debugging.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top