String was not recognized as a valid DateTime in asp.net [Resolved]

Posted by Dn2010 under ASP.NET on 4/4/2012 | Points: 10 | Views : 20295 | Status : [Member] | Replies : 16
hi friends,

String was not recognized as a valid DateTime.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

thanks.




Responses

Posted by: Balajikcp8 on: 4/5/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
pls change the function order

filldate();----->1st
fillgrid();------>2nd

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Balajikcp8 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
what is your Label1.Text value?

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dn2010 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
my Label1.value is

2012-04-03 22:44:42.000

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Balajikcp8 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
Are you getting error on line no 83..?

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dn2010 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
please go through the code and u get idea,


datetime in Label1 from db, compare the date of Label1(last_datetime) in gridview and View records after the Label1(last_datetime) datetime.

please correct the code,

string CommandText = "SELECT DATE_FORMAT(calldate,'%Y%-%m-%d %H:%I:%s.000') as calldate,userfield FROM data WHERE calldate BETWEEN '" + last_datetime + "' And CURDATE() Order By calldate desc limit 20";


1: filled date in Label1.text in page loag

SqlDataAdapter da = new SqlDataAdapter("select top 1 calldate from data", con);
DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
Label1.Text = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff }", Convert.ToDateTime(dt.Rows[0][0].ToString()));




public void fillgrid()
{
DateTime last_datetime = Convert.ToDateTime(Label1.Text);


string ConnectionString = @"driver={MySQL ODBC 3.51 Driver};server=" + ServerName + ";database=" + databasename + ";uid=" + UserName + "; pwd= " + password + "; ";

string CommandText = "SELECT DATE_FORMAT(calldate,'%Y%-%m-%d %H:%I:%s.000') as calldate,userfield FROM data WHERE calldate BETWEEN '" + last_datetime + "' And CURDATE() Order By calldate desc limit 20";


OdbcConnection myConnection = new OdbcConnection(ConnectionString);
OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);

myConnection.Open();

GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();

myConnection.Close();
}


please give the solution for this.

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Balajikcp8 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
why can't you try this to convert and compare date

SELECT * FROM data WHERE cast(calldate as datetime) > '" + last_datetime + "

or

SELECT cast(calldate as datetime) as calldate,userfield FROM data WHERE calldate BETWEEN '" + last_datetime + "' And CURDATE() Order By calldate desc limit 20";

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dn2010 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
i used breakpoint,

DateTime last_datetime = Convert.ToDateTime(Label1.Text);

my first problem is, if i checked ,display last_datetime is " "
but normally
my Label1.value is

2012-04-03 22:44:42.000

string CommandText = "SELECT DATE_FORMAT(calldate,'%Y%-%m-%d %H:%I:%s.000') as calldate,userfield FROM data WHERE calldate BETWEEN '" + last_datetime + "' And CURDATE() Order By calldate desc limit 20";

please give the solution.

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Balajikcp8 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
sorry..Thats not possible..it should display "2012-04-03 22:44:42.000"

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dn2010 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
please give any other solution.

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Balajikcp8 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
pls attach your .cs file..

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dn2010 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
database and examples and i attached csfile


please give the solution.

thanks.
 Download source file

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Balajikcp8 on: 4/5/2012 [Member] Starter | Points: 25

Up
0
Down
Have you got solution?

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dn2010 on: 4/5/2012 [Member] Starter | Points: 25

Up
0
Down
yes , i already rectified that problem,



thank you so much for your help.

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 6/13/2013 [Member] Starter | Points: 25

Up
0
Down
This is my code
Textboxdata :6/14/2013
I am getting following ERROR:String was not recognized as a valid DateTime.
SqlParameter validUntil = new SqlParameter("@ValidUntil", SqlDbType.Date);
validUntil.Value = DateTime.ParseExact(TextBox2.Text.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

cmd.Parameters.Add(validUntil);

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 6/13/2013 [Member] Starter | Points: 25

Up
0
Down
tHIS IS MY CODE
I am inserting date value to database
My database column datatype Date

SqlParameter validUntil = new SqlParameter("@ValidUntil", SqlDbType.Date);
//validUntil.Value = TextBox2.Text.ToString();
validUntil.Value = Convert.ToDateTime(TextBox2.Text.ToString());
cmd.Parameters.Add(validUntil);
I am getting error like this:::String was not recognized as a valid DateTime in asp.net

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/11/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

try this

DateTime date;
string FrDt = "";
if (DateTime.TryParseExact(TxtFr.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
FrDt = date.ToString("yyyy/MM/dd");
}


Mark as Answer if its helpful to you

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response