Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

Input string was not in a correct format

I am reading data from a table and and determined that one of the fields is a blank.  My variable I am trying to store this data to is an integer.  I am doing a Convert.ToInt32 but it does not know how to handle a blank field.  I tried putting a .ToString at the end of the data but that did not work either.  Can someone tell me the syntax to hand blank fields?
MyGlobalVars.initGuid = Guid.NewGuid();
                myMPI.MPI1 = MyGlobalVars.initGuid;
 
                myMPI.NPI = Convert.ToInt32(patient.d.NPI.ToString());--------  Error Here

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of reb73
reb73
Flag of Ireland 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
Try

myMPI.NPI = Convert.ToInt32(IIf(String.IsNullOrEmpty(patient.d.NPI.ToString),0,patient.d.NPI.ToString)

This will supply a default value (e.g., zero) if the field is blank.

reb's is the cleanest route, '??' means if the left side if it is not null, but if it is null, use the right side
Avatar of kwh3856

ASKER

Reb,
Thank you very much.