asp.net - How to handle an exception in VB code if the stored procedure doesn't return one of the fields? -


i have vb function calls stored procedure 2 kinds of reports (actives users/inactive users). stored procedure returns data (columns) based on report choose. if choose active users report, sproc doesn't return inactive_date users, return same field, inactive_date field inactive users report. i'm getting error when choose active users report, because sproc doesn't return inactive_date field, vb.net code same both active/inactive users reports. here of ways tried resolve, no luck.

if isdbnull(dr("inactive_date"))     result.inactive_date = nothing else     result.inactive_date = safestr(dr("inactive_date")) end if 

and

if isdbnull(dr("inactive_date"))     result.inactive_date = datetime.now else     result.inactive_date = safestr(dr("inactive_date")) end if 

you're getting exception because you're trying access dictionary entry doesn't exist. have few options.

  1. have stored procedure active users return inactive_date null. allow use code have posted in vb no changes.
  2. create function checks if datareader has column before attempt access it, in answer: check column name in sqldatareader object - akin checking if dictionary has key first
  3. write 2 separate functions in vb code - 1 active report , 1 inactive report. or, add parameter function check if request active user, , try access "inactive_date" field if parameter set true.

if me, i'd update stored procedure (option 1). least amount of work, shouldn't hurt semantically (unless have queries select * results of procedure), , requires no other changes codebase. other problem can see confusion might arise having function in vb code that's used both active , inactive users.

the second option involve iterating through fields, , feels wrong me (seems it's asking performance issues if used incorrectly). it's least invasive though if can't change stored proc.

the third option creates more problems elsewhere in code base. said, shouldn't have 1 function 2 different things - i'd avoid parameterized version if end going down route, , go 2 separate functions.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -