0
votes
Send C# datetime to SQL Server - error "Conversion failed when converting date and/or time from character string" - Stack Overflow
Asked
Viewed 3k times
0

I am trying to submit a form that contains a few datetimepickers as well as other controls. other controls have no problem but when I take the datetimepicker date value and put it in a parameterised insert statement, this error happens:

Conversion failed when converting date and/or time from character string

I have tried the following methods:

  1. custom datetime format:

    dateTimePicker1.Format = DateTimePickerFormat.Custom;
    dateTimePicker1.CustomFormat = "dd/MM/yyyy";'  
    
  2. convert it to string and then into date time using datetime.parseexact, datetime.parse and Convert.ToDateTime.

  3. tried this type of code as well:

    DateTime date2 = Convert.ToDateTime(dateTimePicker2.Text);
    

Full code

con.Open();

string s = "insert into [NutritionClinicDatabase].[dbo].[PatientInformation] values(@NIC, @ClinicNo, @PatientCategory, @Date, @Name, @Gender, @DOB, @DateOfAdmission, @DateOfDischarge, @DurationOfVisit, @Age, @City, @Contact, @Height, @Weight, @UsualWweight, @ReferredUnit, @WC, @FatMass, @LeanBodyMass, @HandGripStrength, @Intake, @Illness, @Illness2, @Illness3, @Illness4, @Specify, @Other)";


cmd = new SqlCommand(s, con);
String strDateFormat = "yyyy-MM-dd";
cmd.Parameters.AddWithValue("@NIC", NIC);
cmd.Parameters.AddWithValue("@ClinicNo", ClinicNo);
cmd.Parameters.AddWithValue("@PatientCategory", PatientCategory);

if (PatientCategory == "Clinic Patient")
{
    cmd.Parameters.AddWithValue("@Date", SqlDbType.Date).Value = dateTimePicker1.Value.Date;
}
else if ((PatientCategory == "In-Ward Patient") || (PatientCategory == "ICU Patient"))
{
    cmd.Parameters.AddWithValue("@Date", DBNull.Value);
}

cmd.Parameters.AddWithValue("@Name", Name1);
cmd.Parameters.AddWithValue("@Gender", Gender);

//if (dateTimePicker2.CustomFormat == " ")
//    cmd.Parameters.AddWithValue("@DOB", DBNull.Value);
//else
cmd.Parameters.AddWithValue("@DOB", SqlDbType.Date).Value = dateTimePicker2.Value.Date;
// cmd.Parameters.AddWithValue("@DOB", SqlDbType.Date).Value = dateTimePicker2.Value.Date;

if ((PatientCategory == "In-Ward Patient") || (PatientCategory == "ICU Patient"))
{
    //if (dateTimePicker3.CustomFormat == " ")
    //    cmd.Parameters.AddWithValue("@DateOfAdmission", DBNull.Value);
    //else
    cmd.Parameters.AddWithValue("@DateOfAdmission", SqlDbType.Date).Value = dateTimePicker3.Value.Date;
    // cmd.Parameters.AddWithValue("@DOB", SqlDbType.Date).Value = dateTimePicker2.Value.Date;
    //if (dateTimePicker4.CustomFormat == " ")
    //    cmd.Parameters.AddWithValue("@DateOfDischarge", DBNull.Value);
    //else
    cmd.Parameters.AddWithValue("@DateOfDischarge", SqlDbType.Date).Value = dateTimePicker4.Value.Date;
    //cmd.Parameters.AddWithValue("@DateOfDischarge", DateTime.Parse(dateTimePicker4.Value.Date.ToString()));
    // cmd.Parameters.AddWithValue("@DOB", SqlDbType.Date).Value = dateTimePicker2.Value.Date;

    cmd.Parameters.AddWithValue("@DurationOfVisit", Duration);
}
else if (PatientCategory == "Clinic Patient")
{
    cmd.Parameters.AddWithValue("@DateOfAdmission", DBNull.Value);
    cmd.Parameters.AddWithValue("@DateOfDischarge", DBNull.Value);
    cmd.Parameters.AddWithValue("@DurationOfVisit", "Not Specified");
}

cmd.Parameters.AddWithValue("@Age", Age);
cmd.Parameters.AddWithValue("@City", City);
cmd.Parameters.AddWithValue("@Contact", Contact);
cmd.Parameters.AddWithValue("@Height", Height1);
cmd.Parameters.AddWithValue("@Weight", Weight);
cmd.Parameters.AddWithValue("@UsualWweight", UsualWeight);
//cmd.Parameters.AddWithValue("@CurrentWeight", CurrentWeight);
cmd.Parameters.AddWithValue("@ReferredUnit", ReferredUnit);
cmd.Parameters.AddWithValue("@WC", WC);
cmd.Parameters.AddWithValue("@FatMass", FatMass);
cmd.Parameters.AddWithValue("@LeanBodyMass", LeanBodyMass);
cmd.Parameters.AddWithValue("@HandGripStrength", HandGripStrength);
cmd.Parameters.AddWithValue("@Intake", Intake);
cmd.Parameters.AddWithValue("@Illness", Illness);
cmd.Parameters.AddWithValue("@Illness2", Illness2);
cmd.Parameters.AddWithValue("@Illness3", Illness3);
cmd.Parameters.AddWithValue("@Illness4", Illness4);
cmd.Parameters.AddWithValue("@Specify", Specify);
cmd.Parameters.AddWithValue("@Other", Other);

cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show("Patient Registered");
1
4

You are not using AddWithValue correctly. The AddWithValue method takes a parameter name and an object that is the parameter value, not the type. I suggest you avoid AddWithValue entirely and instead use the Add method, such as:

cmd.Parameters.Add("@Date", SqlDbType.Date).Value = dateTimePicker1.Value.Date;
1
  • will try after i go to office and come.
    – Dusk Hare
    Apr 10 2019 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.

 
1
Best is to avoid AddWithValueGuidoG

1 Answers

4
votes

You are not using AddWithValue correctly. The AddWithValue method takes a parameter name and an object that is the parameter value, not the type. I suggest you avoid AddWithValue entirely and instead use the Add method, such as:

cmd.Parameters.Add("@Date", SqlDbType.Date).Value = dateTimePicker1.Value.Date;