Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
My query is when I created web setup of my web application. And then run it on my computer it runs fine but when I enter fromdate(dd/MM/yyyy) and todate(dd/MM/yyyy) in textbox and then submit it, then it gives an error that string was not recognized as a valid datetime.
 
I want to enter date format like dd/MM/yyyy in textbox and save yyyy-MM-dd in database.
 
The error page is shown below.
 

Server Error in '/DT' Application.
--------------------------------------------------------------------------------
 
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.
 
Source Error: 
 

Line 23:         //DateTime dt1 = Convert.ToDateTime(TextBox1.Text.Trim());
Line 24:         //DateTime dt2 = Convert.ToDateTime(TextBox2.Text.Trim());
Line 25:         string dt1 = Convert.ToDateTime(TextBox1.Text.Trim()).ToString();
Line 26:         string dt2 = Convert.ToDateTime(TextBox2.Text.Trim()).ToString();
Line 27:         SqlConnection conn = new SqlConnection("Data Source=MAX9;Initial Catalog=Leave;User ID=sa;pwd=;");
 
 
Source File: c:\Inetpub\wwwroot\DT\Default2.aspx.cs    Line: 25 
 
Stack Trace: 
 

[FormatException: String was not recognized as a valid DateTime.]
   System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +2838082
   System.Convert.ToDateTime(String value) +98
   Default2.Button1_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\DT\Default2.aspx.cs:25
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
 
 
 

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3615; ASP.NET Version:2.0.50727.3618 
 
Source Code and stored procedure which I used on Send button
protected void Button1_Click(object sender, EventArgs e)
{
        
        string dt1 =Convert.ToDateTime(TextBox1.Text.Trim()).ToString();
        string dt2 =Convert.ToDateTime(TextBox2.Text.Trim()).ToString();
        SqlConnection conn = new SqlConnection("Data Source=MAX9;Initial Catalog=Leave;User ID=sa;pwd=;");
        SqlCommand cmd = new SqlCommand("insert_testtable", conn);
        conn.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@FromDate", SqlDbType.DateTime).Value = dt1;
        cmd.Parameters.Add("@ToDate", SqlDbType.DateTime).Value = dt2;
      
 
        try
        {
            cmd.ExecuteNonQuery();
            lblMessage.Text = "Record inserted successfully";
        }
        catch (Exception ex)
        {
            throw ex;
        }
        conn.Close();
 

         }
}
 
Stored Procedure
CREATE PROCEDURE [dbo].[insert_testtable]
@FromDate datetime,
@ToDate datetime
 AS
insert into testtable values (@FromDate,@ToDate)
GO
 
Please solve my query as soon as possible. Thanks in advance.
Posted 11-Jan-11 22:24pm
Edited 11-Jan-11 22:32pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

In this what I should pass in format and Provider
 
Pls explain through example.
 
Pls send me C# code.
 
I really need this .
  Permalink  
Comments
Estys at 12-Jan-11 6:09am
   
See example in link : http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
You should use "Add Comment" instead of "Answer" if you need clarification about something.
Cheers
Manjula.c.m at 20-Sep-11 7:19am
   
Pls explain through example.

Pls send me C# code.

I really need this .
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use the following code:
 
string format = "dd/MM/yyyy"; 
DateTime dt = DateTime.ParseExact(dateString, format, provider);
 
If you expect the user to give input in format "MM/dd/yyyy", you need to use the following code:
 
string format = "MM/dd/yyyy"; 
DateTime dt = DateTime.ParseExact(dateString, format, provider);
 
You don't need to worry about in which format the DateTime object is being saved in the Database, as long as only your application retrieves the same DateTime data.
  Permalink  
Comments
Dalek Dave at 12-Jan-11 5:03am
   
Good Answer.
Member 8569972 at 18-Jan-12 0:58am
   
Hello I am using your following code but it gives error for provider, so please can you tell me what is provider?
 
string format = "dd/MM/yyyy";
DateTime dt = DateTime.ParseExact(dateString, format, provider);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you should use DateTime.Parse(String, IFormatProvider) :
http://msdn.microsoft.com/en-us/library/kc8s65zs.aspx[^]
 
the line
cmd.Parameters.Add("@FromDate", SqlDbType.DateTime).Value = dt1;
 
is plain wrong, don't use a string, use a datetime.
 
Cheers
  Permalink  
Comments
Dalek Dave at 12-Jan-11 5:03am
   
Well Said.

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

  Print Answers RSS
0 CPallini 510
1 Sergey Alexandrovich Kryukov 428
2 George Jonsson 298
3 Pheonyx 218
4 OriginalGriff 200
0 OriginalGriff 4,813
1 CPallini 3,850
2 Sergey Alexandrovich Kryukov 3,224
3 George Jonsson 2,552
4 Gihan Liyanage 2,186


Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 12 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100