我得到这个例外几次,但在我运行的其他时间工作 [英] I Get This Exception Few Times ,But Works During Other Time I Run

查看:113
本文介绍了我得到这个例外几次,但在我运行的其他时间工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);



我得到这个例外一段时间不知道为什么,



增加或减去的值会导致无法表示的DateTime。

参数名称:value



描述:未处理的异常在执行当前Web请求期间发生。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.ArgumentOutOfRangeException:添加或减去的值导致无法表示的DateTime。

参数名称:value



来源错误:






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:


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

推荐答案

1。当您的日历控件的日期值为 DateTime.MinValue 时,将抛出此异常,因为您尝试从最小值中减去1。详情请见此处。 [ ^ ]



2.解决方案是管理此案例,您有两种选择:

a)使用您的值创建日历控件首次使用前的代码;

b)如果当前值为 DateTime.MinValue ,则使用IF管理特殊情况,在这种情况下执行不执行减法。
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.


这篇关于我得到这个例外几次,但在我运行的其他时间工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆