2

I am getting an issue with saving data from a datepicker to my ms sql database. My column in MSSQL is set to [datetime]. I have this code but I'm getting error in saving because it's unable to convert date/time to correct format. can you help me. Thank you.

The Error is: Conversion failed when converting date and/or time from character string.

protected void RadButton1_Click(object sender, EventArgs e)
{
            string CS = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {

                string str = "insert into Leave_Application values(@Badge_Number, @Name, @Section, @Position, @Date_Hired, @Leave_Type, @Date_From, @Date_To, @Time_From, @Time_To, @Reason, '0', getdate(), 'NULL', ' ' )";
                con.Open();
                cmd = new SqlCommand(str, con);

                cmd.Parameters.AddWithValue("@Badge_Number", lblBadgeNo.Text);
                cmd.Parameters.AddWithValue("@Name", lblName.Text);
                cmd.Parameters.AddWithValue("@Section", lblSect.Text);
                cmd.Parameters.AddWithValue("@Position", lblPos.Text);
                cmd.Parameters.AddWithValue("@Date_Hired", lblDateEmp.Text);
                cmd.Parameters.AddWithValue("@Leave_Type", RadioButtonList1.SelectedValue);
                cmd.Parameters.AddWithValue("@Date_From", Convert.ToDateTime(RadDatePicker1.SelectedDate));
                cmd.Parameters.AddWithValue("@Date_To", Convert.ToDateTime(RadDatePicker2.SelectedDate));
                cmd.Parameters.AddWithValue("@Time_From", Convert.ToDateTime(RadTimePicker1.SelectedDate));
                cmd.Parameters.AddWithValue("@Time_To",  Convert.ToDateTime(RadTimePicker1.SelectedDate);
                cmd.Parameters.AddWithValue("@Reason", txtReason.Text);

                cmd.ExecuteNonQuery();
                con.Close();

 }
}
4
  • You should debug through your code and see if the SelectedDate values are not null and matching the expected format. Commented Jul 4, 2013 at 11:38
  • 1
    As a side remark, it would be better to name the fields in your insert query. if you add or remove a field to your table, your current query would be broken. Commented Jul 4, 2013 at 11:38
  • Maybe it's because of the Time_From and Time_To? Or are they in [datetime] format also? Commented Jul 4, 2013 at 11:40
  • Can you share the error that you are getting Commented Jul 4, 2013 at 11:46

6 Answers 6

1

The reason could be the format of date in mysql and C# are different.
You will need the same format for sending values to the function.

You can use the following format

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

DateTime.Now.ToString("yyyyMMddHHmmss");

In your case

Convert.ToDateTime(RadDatePicker1.SelectedDate).ToString("yyyy-MM-dd HH:mm:ss");
Sign up to request clarification or add additional context in comments.

Comments

0

First you remove the convert.todatetime then check. Because in sql its already datetime. so here no need of conversion.

13 Comments

i think he's converting his input string, not the sql server column
RadDatePicker1.SelectedDate means its datetime only. then why he is again converting..
Prasad thank you but still I'm getting the same error with this cmd.Parameters.AddWithValue("@Date_From", RadDatePicker1.SelectedDate);
use this code Convert.ToDateTime(RadDatePicker1.SelectedDate).ToString("yyyy-MM-dd HH:mm:ss");
that one seems to be working. how about the RADTIMEPICKER? what's the conversion since I'm getting error on that one. Thanks.
|
0

My guess is that you are passing the datetime in the incorrect locale format. It may be worth debugging the code to see what sqlserver is expecting vs what you are supplying. Typically, I have found that this is usually a case of the day and month portions of the passed date being the alternate format.

You can also try a ToString() format on the input, putting it into the expected culture/locale that you expect.

you can also try it from the client end of things:

<telerik:RadDatePicker ID="RadDatePicker1" DateInput-DateFormat="MM/dd/yyyy">
</telerik:RadDatePicker>

(or whatever date format tickles your fancy)

2 Comments

i am getting this error --- Conversion failed when converting date and/or time from character string.
see prasads answer, that may be of help
0

One solution could be to pass mysql datetime format as well

i.e

cmd.Parameters.AddWithValue("@Time_From",
"date_format(" +  (RadTimePicker1.SelectedDate.ToString("yyyy-MM-dd HH:mm:ss") 
+ ",'%Y-%m-%d %h:%i:%s')" ));

Comments

0

I Think you should add the column names too in insert query, because if the sequence is not correct then this may happen that you are passing the wrong value to date time column.

Comments

0

This must be due to a difference in datetime format in MSSQL server and C#. I have seen this happening in EF also when I try to run applications on different versions of SQL server.

Since you have not mentioned the version of your sql server, I would suggest you to run the SQL server profiler while running the insert command.

Then get the SQL statement from the SQL server profiler and compare the dateformat in your SQL query against your SQL server dateformat.

I am sure you will be able to find the solution after that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.