Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        SqlConnection con = new SqlConnection(CS);
        SqlCommand cmd = new SqlCommand("spHoteLMaster", con);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter days = new SqlParameter("@Days",Label1.Text);
        SqlParameter checkin = new SqlParameter("@Checkin",Calendar1.SelectedDate.ToShortDateString());
        SqlParameter checkout = new SqlParameter("@Checkout",Calendar2.SelectedDate.AddDays(-1).ToShortDateString());

        cmd.Parameters.Add(days);
        cmd.Parameters.Add(checkin);
        cmd.Parameters.Add(checkout);

I get this exception some time dont know why,

The added or subtracted value results in an un-representable DateTime.
Parameter name: value

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.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime.
Parameter name: value

Source Error:


C#
Line 138:        SqlParameter days = new SqlParameter("@Days",Label1.Text);
Line 139:        SqlParameter checkin = new SqlParameter("@Checkin",Calendar1.SelectedDate.ToShortDateString());
Line 140:        SqlParameter checkout = new SqlParameter("@Checkout",Calendar2.SelectedDate.AddDays(-1).ToShortDateString());
Line 141:
Line 142:        cmd.Parameters.Add(days);

Source File: e:\Php\ASP PROJECTS\Hotel\Default.aspx.cs    Line: 140
Posted
Updated 11-Mar-15 20:47pm
v3
Comments
Thanks7872 12-Mar-15 2:42am    
Error is clear enough : The added or subtracted value results in an un-representable DateTime. Use breakpoint and verify all the values at the line you got the error.

1 solution

1. When your calendar control have the date value as DateTime.MinValue, this exception will be thrown, because you are trying to subtract 1 from min value. See details here.[^]

2. The solution is to manage this case, and you have two options:
a) Init your calendar control with a value in your code before its first use;
b) Manage the special case by using IF and testing if the current value is DateTime.MinValue and in this case do not perform the subtraction.
 
Share this answer
 

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