Share via


Convert date to datetime c#

Question

Tuesday, June 10, 2014 8:49 AM

Hi,

I have a gridview with the column displaying date value. But in DB its datetime datatype.

Label date = (Label)GridView1.Rows[e.RowIndex].FindControl("Date");

In my screen the date value displays like `2014-05-3O` in the grid and in database the value is like `2014-05-30 00:00:00.000`, where I converted datetime to date to show it in the screen. Now I m trying to delete the row and it gives an error. The problem is how can I convert this date to datetime, so that I can pass the string in the delete statement.

Anyone help me with this?

 

All replies (6)

Tuesday, June 10, 2014 9:05 AM âś…Answered

You can try with the below code

DateTime ConvertedVal = DateTime.ParseExact(date, "yyyy-MM-dd", null);

Tuesday, June 10, 2014 9:01 AM

Convert.ToDateTime("2014-05-3O")


Tuesday, June 10, 2014 9:37 AM

Hi,

That worked.
I have another column where the datatype is varchar and it has values like 1016, 1023T and when I try to delete I get the error.

Label ID =  (Label)GridView1.Rows[e.RowIndex].FindControl("lblID");

Error converting data type varchar to numeric.


Tuesday, June 10, 2014 9:41 AM

You can pass @date parameter instead of passing a string. With or without 00:00... the date value both are same.


Tuesday, June 10, 2014 9:46 AM

I have another column where the datatype is varchar and it has values like 1016, 1023T and when I try to delete I get the error.

Label ID =  (Label)GridView1.Rows[e.RowIndex].FindControl("lblID");

Error converting data type varchar to numeric.

As the error message states you cannot convert a Varchar datatype value to Numeric value, when the value contains alpahabets(1023T).

Your best option is to keep the DataType as Varchar for SQL Server and String for C# code.


Tuesday, June 10, 2014 9:49 AM

is there any way I can convert this?