3

I'm trying to add a datetime into a database table by grabbing values from textboxes that have various masks etc. I'm getting the following error:

String was not recognized as a valid DateTime.

Textbox code:

<asp:TableRow>
        <asp:TableCell>    
                <asp:ToolkitScriptManager ID="calendarToolkit" runat="server"></asp:ToolkitScriptManager>   
                <asp:TextBox ID="startDateTextbox" runat="server"></asp:TextBox>  
                <asp:CalendarExtender Format="yyyy-dd-MM"  ID="startDateExtender" TargetControlID="startDateTextbox" runat="server" /> 
        </asp:TableCell>
    </asp:TableRow>

    <asp:TableRow>
        <asp:TableCell>    
                <asp:TextBox ID="startTimeTextbox" runat="server"></asp:TextBox>  
                <asp:MaskedEditExtender ID="m1" runat="server" Mask="99:99" MaskType="Time" AcceptAMPM="false" 
                                    MessageValidatorTip="true" TargetControlID="startTimeTextbox"
                                    ClearMaskOnLostFocus="false">
                </asp:MaskedEditExtender>

                <asp:MaskedEditValidator ID="mv1" runat="server" ControlExtender="m1" ControlToValidate="startTimeTextbox"
                                    Display="none" EmptyValueMessage="Time is required" InvalidValueMessage="Valid Start Time"
                                    IsValidEmpty="true" TooltipMessage="Input a time">
                </asp:MaskedEditValidator>
        </asp:TableCell>
    </asp:TableRow>

c# code to add values into database:

string sqlQuery =
                "insert into dbo.Event_Info(event_name, description, location, date_end,date_start,image_url) values(@name, @desc, @location,@startDate,@endDate,@imageURL)";

SqlConnection dbConnection = new SqlConnection("server=(local)\\SGSQL;database=STEvent;Trusted_Connection=yes");
            dbConnection.Open();
            SqlCommand dbCommand = new SqlCommand(sqlQuery, dbConnection);

SqlParameter startParam = new SqlParameter("@startDate",SqlDbType.DateTime);
            startParam.Value = Convert.ToDateTime(startDateTextbox.Text +" " + startTimeTextbox.Text);

            dbCommand.Parameters.Add(startParam);
            dbCommand.ExecuteNonQuery();

            dbConnection.Close();

Note: Only added a subset of the code as showing all the other parameters seemed long and redundant.

2
  • What was the imput in the two textboxes? Commented Dec 5, 2012 at 12:20
  • @Derek 2012-01-12 and 11:00 Commented Dec 5, 2012 at 12:21

2 Answers 2

3

Try to parse your String to DateTime.

// String to DateTime
 String MyString;
 MyString = "1999-09-01 21:34 PM";
 //MyString = "1999-09-01 21:34 p.m.";  //Depends on your regional settings

 DateTime MyDateTime;
 MyDateTime = new DateTime();
 MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",
                                  null)
Sign up to request clarification or add additional context in comments.

Comments

1

Date format "yyyy-dd-MM" is invalid. You can lookup the "Convert" function in SQL Server documentation for valid formats.

"yyyy-MM-dd" is a good solution

1 Comment

Thank you, that worked and it's very simple. I'll accept your answer when I'm allowed to!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.