Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I want to insert the date into sql database it shows error
String was not recognized as a valid DateTime
.Please help me out how to insert date into sql server by using mvc franework.

This is the sql table
C#
SELECT TOP 1000 [Id]
      ,[FirstName]
      ,[LastName]
      ,[Email]
      ,[TelephoneNo]
      ,[Category]
      ,[Specifcation]
      ,[BuyBetweenDatesFrom]
      ,[BuyBetweenDatesTo]      
      ,[Details]
  FROM [Demo].[dbo].[Enquiry]


thsi is the CSHTML Code

HTML
@Html.TextBoxFor(x => x.BuyBetweenDatesFrom, new { @placeholder = "Select From Date ", id = "datepickerfrom", @class = "form-control" })
 @Html.TextBoxFor(x => x.BuyBetweenDatesTo, new { @placeholder = "Select To Date ", id = "datepicker", @class = "form-control " })


This is the jquery code for datetime picker.

<script>
$(function () {
$("#datepicker").datepicker();
});
</script>
<script>
$(function () {
$("#datepickerfrom").datepicker();
});
</script>

This is the Controller code.
C#
public ActionResult ProductInquiry(Enquirya)
        {
         
           Enquiry enquiry = new Enquiry();
           enquiry.FirstName=Request.Form["FirstName"];
           enquiry.LastName = Request.Form["LastName"];
           enquiry.Email=Request.Form["Email"];
           enquiry.TelephoneNo = Request.Form["TelephoneNo"];
           enquiry.Category=Request.Form["Category"];
           enquiry.Specifcation=Request.Form["Specification"];
           enquiry.BuyBetweenDatesFrom =Convert.ToDateTime(Request.Form["BuyBetweenDatesFrom"].ToString());
           enquiry.BuyBetweenDatesTo =Convert.ToDateTime(Request.Form["BuyBetweenDatesTo"].ToString());
Posted
Updated 23-Dec-15 0:46am
v6

1 solution

The main problem is that you are not checking the value of the Request.Form variable. If that value is not in a correct format (or is empty), then your code will fail on the Convert.ToDateTime() call. Don't forget that just because you're allowing for the use of the jquery.datepicker to be used, doesn't mean that the data will be selected with a click of the mouse and that the value doesn't need to be validated before the conversion.

Based on this, you could change your Convert.ToDateTime to be a DateTime.TryParse call and you would be able to know whether the parse worked properly.

The real question here is why your POST isn't just using the Enquiry model that you're accepting as a parameter. This information will be populated by default based on the types of Html helpers you are using. Additionally, if your model defines those two fields as being 'DateTime?', it will be able to handle null values.
 
Share this answer
 
Comments
@shok kumar mishra 24-Dec-15 3:09am    
enquiry.BuyBetweenDatesTo = DateTime.TryParse(Request.Form["BuyBetweenDatesTo"]);
By using this "DateTime.TryParse" error is there. It shows "No Overload For Method 'TryParse' takes 1 arguments"
Bryan Tubbs 28-Dec-15 10:26am    
The DateTime.TryParse actually takes two parameters and would be called as: DateTime.TryParse(Request.Form["BuyBetweenDatesTo"], out buyBetweenDatesTo).

You would need to define the output parameter. Or you could possibly just use the model -- entity.BuyBetweenDatesTo. If the parse was successful, it would go directly into your model.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900