Pinchas K
Pinchas K

Reputation: 1571

A datetime format error

I am getting an exception error, "Input string was not in a correct format." "Make sure your method arguments are in the right format. When converting a string to DateTime, parse the string to take the date beforeputting each varibale into the DateTime object".

it is pointing on the last line of this code:

cnaIntakeVSOutputDetail_Intake.Add(new IntakeVsOutput(
facility,
UnitRepository.Units(facility).SingleOrDefault(u => u.Code.Equals(reader.To("UNIT_CODE", ""))),
new Patient()
{
     Account = reader.To("PATIENT_ID", ""),
     MRN = reader.To("MRN", ""),
     Name = reader.To("NAME_FULL", ""),
     Room = reader.To("ROOM_BED", ""),
 },
     reader.To("INFO", ""),
     Convert.ToDateTime(reader["TRDATE"]),
     Convert.ToInt32(reader["MENU_ID"]),
     Convert.ToInt32(reader["VALUE"]),
     null, null, null));

Upvotes: 0

Views: 705

Answers (3)

JSJ
JSJ

Reputation: 5691

first of all you need to ensure that your TRDATE column contains values else it will fire the error the input not in correct format. for that you can use the

string.IsNullorEmpty(reader["TRDATE"].ToString())

method and then you need to parse the values other wise set your default values or leave it null to return type.

Upvotes: 0

Behnam Esmaili
Behnam Esmaili

Reputation: 5967

try DateTime.TryParse instade:

string dateString;
 DateTime dateValue;
 DateTime.TryParse(dateString, out dateValue);

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56717

reader["TRDATE"] does not contain a string that Convert.ToDateTime could convert into a DateTime value.

This might be because it is an empty string or DBNull.Value or null.

Upvotes: 3

Related Questions