user3436232
user3436232

Reputation: 53

Failed convert datetime,date,time using string?

I get an error about datetime,date and time.. i got 3 different datatype , mostly error all of them.. how do i get it right?

this datatype datetime: i dont know sometime i got it worked and some dont..

comu.Parameters.Add("Casedate", SqlDbType.DateTime);
comu.Parameters["Casedate"].Value = Convert.ToDateTime(TextBox1.Text);

this are datatype date only:

comu.Parameters.Add("Startdate", SqlDbType.Date);
comu.Parameters["Startdate"].Value = Convert.ToString(TextBox12.Text);

comu.Parameters.Add("Enddate", SqlDbType.Date);
comu.Parameters["Enddate"].Value = Convert.ToString(TextBox13.Text);

this are datatype time:

comu.Parameters.Add("Starttime", SqlDbType.Time);
comu.Parameters["Starttime"].Value = Convert.ToString(TextBox14.Text);

comu.Parameters.Add("Endtime", SqlDbType.Time);
comu.Parameters["Endtime"].Value = Convert.ToString(TextBox15.Text);

now i have problem on datetime, but in the last page i got it worked by put in data 11/11/2014 data in it... but now it show error on datetime back

Upvotes: 0

Views: 696

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

if you know the DateTime format upfront you can use DateTime.ParseExact() method to parse the given date time value properly

using System.Globalization;

DateTime dt = DateTime.ParseExact(datestring, "dd/MM/yyyy 
                                  hh:mm:ss",CultureInfo.InvariantCulture);

Upvotes: 2

bobthedeveloper
bobthedeveloper

Reputation: 3783

You should use the DateTime.Parse() method, you are converting a string to a string.

DateTime.Parse(TextBox1.Text)

Upvotes: 1

Related Questions