Click here to Skip to main content
15,065,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All ,

I try to convert textbox value into time .

I have this value in text box
17/02/2020 12:00:00 AM


when I save this I got this error ,

System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to datetime.'


I know it must convert it from string to but how I can add this to my code

could an one help me please

Thanks

What I have tried:

This my code

cmd.Parameters.AddWithValue("@StartDate", txtStartDate.Text.Trim());


and another one

txtStartDate.Text = dt.Rows[0]["StartDate"].ToString();
Posted
Updated 12-Jan-21 22:41pm

1 solution

Try:
C#
DateTime startDate;
if (DateTime.TryParse(txtStartDate.Text, out startDate))
{
    cmd.Parameters.AddWithValue("@StartDate", startDate);
}
DateTime.TryParse Method (System) | Microsoft Docs[^]

Depending on your server's cultural settings, you may need to use TryParseExact instead:
DateTime.TryParseExact Method (System) | Microsoft Docs[^]
   
Comments
Maciej Los 13-Jan-21 4:42am
   
5ed!
coderom2019 13-Jan-21 4:51am
   
Thanks it is work
coderom2019 13-Jan-21 4:52am
   
could you help me with this also ,

txtStartDate.Text = dt.Rows[0]["StartDate"].ToString();
Richard Deeming 13-Jan-21 4:58am
   
Help with what, precisely?
coderom2019 14-Jan-21 0:55am
   
Thank you , I have solve it .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900