locked
String was not recognized as valid datetime error RRS feed

  • Question

  • User-1820614617 posted

    Hi,

    I have a textbox with AJAX calendar tied to it. I am trying to enter the value of this textbox into SQL table column with DateTime format.

    For this I converted the textbox value to Datetime format in C# using the following code: Convert.ToDateTime(txtDate.Text)

    But this gives me a 'String was not recognized as valid datetime error'.I am stuck at this point of code and cannot move forward without solving this issue.

    Please do help me in this.

    Thanks.

    Saturday, October 29, 2011 6:38 PM

Answers

  • User617492218 posted

    1) make sure your SQL table column value is set to datetime

    2) what's the text box displaying? also, put a breakpoint there and see what it's showing. 

    3) better to paste some code when writting in forums, not just 1 line, maybe you made a syntax somewhere and we have to ask to see more of your code

    4) not that it would matter, do 

    DateTime date = DateTime.Parse(txtDate.Text);

    5) make sure you actually have a value inside "txtDate" before trying to parse it - always a good practice to check if the textbox is empty before trying to parse it into anything, or for that matter, saving it to a DB.

    if (!string.IsNullOrEmpty(txtDate.Text)
    {
        DateTime date = DateTime.Parse(txtDate.Text);
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 29, 2011 6:52 PM

All replies

  • User617492218 posted

    1) make sure your SQL table column value is set to datetime

    2) what's the text box displaying? also, put a breakpoint there and see what it's showing. 

    3) better to paste some code when writting in forums, not just 1 line, maybe you made a syntax somewhere and we have to ask to see more of your code

    4) not that it would matter, do 

    DateTime date = DateTime.Parse(txtDate.Text);

    5) make sure you actually have a value inside "txtDate" before trying to parse it - always a good practice to check if the textbox is empty before trying to parse it into anything, or for that matter, saving it to a DB.

    if (!string.IsNullOrEmpty(txtDate.Text)
    {
        DateTime date = DateTime.Parse(txtDate.Text);
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 29, 2011 6:52 PM
  • User-1820614617 posted

    @robwscott

    Sql table column is set to DateTime datatype and the textbox displays the date in mm/dd/yyyy format. Below is how my insert command looks like:

    syllabus.aspx.cs
    
    SqlCommand cmd = new SqlCommand("Syllabus_Insert", connString);
                try
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(txtDate.Text));
                    return cmd.ExecuteNonQuery();
                }
                catch { throw; }

    syllabus.aspx
    
    <td>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:TextBox ID="txtDate" runat="server" Height="16px" Width="188px"></asp:TextBox>
                            <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate">
                            </asp:CalendarExtender>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
    Saturday, October 29, 2011 6:58 PM
  • User465171450 posted

    In addition to Rob's excellent suggestions, instead of DateTime.Parse you might want to try DateTime.TryParse. TryParse basically takes two parameters, a string, and a DateTime as output. It returns true if success or false if failure which lets you easily test to see if you really have a datetime string like so:

    DateTime tempTime = DateTime.MinValue;
    if(DateTime.TryParse(txtDate.Text,out tempTime))
    {
    // then we really do have a valid date format and it's now stored in the tempTime variable.
    }

    Saturday, October 29, 2011 7:10 PM