值不能为空。参数名称:String我收到此错误

时间:2015-06-23 03:40:52

标签: c# asp.net

protected void Page_Load(object sender, EventArgs e)
        {
            int WeekId = Int32.Parse(Request.QueryString["WeekId"]);
            if (((int)DateTime.Now.DayOfWeek) != WeekId)
            {
                Response.Write("<h4>Sorry! Today is NOT the week you selected. Please use Back button of browser and try again!</h4>");
                Response.End();
            }
            lblToday.Text = DateTime.Now.ToShortDateString();
            lblWeekId.Text = Request.QueryString["WeekId"];
            lblPeriod.Text = Request.QueryString["period"];
            lblSemister.Text = Request.QueryString["Semester"];
        }

3 个答案:

答案 0 :(得分:0)

只需添加您的链接(或喜欢)

xxxThisIsYourLinkxxx?WeekId=yyy

yyy可以更改为整数值(例如1,2或喜欢)

答案 1 :(得分:0)

您的querystring值为null。确保此querystring passed为此页面try catch。另外,为了更好的方法,请将代码放在try{ int WeekId = Int32.Parse(Request.QueryString["WeekId"]); if (((int)DateTime.Now.DayOfWeek) != WeekId) { Response.Write("<h4>Sorry! Today is NOT the week you selected. Please use Back button of browser and try again!</h4>"); Response.End(); } lblToday.Text = DateTime.Now.ToShortDateString(); lblWeekId.Text = Request.QueryString["WeekId"]; lblPeriod.Text = Request.QueryString["period"]; lblSemister.Text = Request.QueryString["Semester"]; } catch(Exception ex) { //Handle exception here } 块中。

Error: [$rootScope:infdig] http://errors.angularjs.org/1.4.1/$rootScope/infdig?p0=10&p1=%5B%5D
   at m.prototype.$digest (https://localhost:44301/angular.min.js:132:501)
   at m.prototype.$apply (https://localhost:44301/Scripts/angular.min.js:135:159)
   at Anonymous function (https://localhost:44301/Scripts/angular.min.js:19:315)
   at e (https://localhost:44301/Scripts/angular.min.js:39:10)
   at d (https://localhost:44301/Scripts/angular.min.js:19:236)
   at zc (https://localhost:44301/Scripts/angular.min.js:20:23)
   at Yd (https://localhost:44301/Scripts/angular.min.js:18:342)
   at Anonymous function (https://localhost:44301/Scripts/angular.min.js:289:159)
   at j (https://localhost:44301/js/jquery/jquery-2.1.1.min.js:2:26852)
   at k.fireWith (https://localhost:44301/js/jquery/jquery-2.1.1.min.js:2:27609)

答案 2 :(得分:0)

只需使用您提供的有限信息快速回答此问题。

首先,您没有提供错误适用的行但是它显然出现在您尝试从查询字符串转换值的第一个语句中,而不检查值是否存在且不为null (因此错误)。现在有很多不同的方法可以解决这个问题,但可以很快解决。

int WeekId = Int32.Parse(Request.QueryString["WeekId"]);&lt; - 假设这是错误行。

基本决议:

int weekId = 0;

//first try and parse the int from the query string. Note the  ?? will execute the statement 
//following when the result is null. https://msdn.microsoft.com/en-us/library/ms173224.aspx
if (!int.TryParse((Request.QueryString["WeekId"] ?? ""), out weekId))
{
    Response.Write("<h4>Sorry! You didnt select a valid week. Please use Back button of browser and try again!</h4>");
    Response.End();
}

//next lets check the value of the day of week.. 
if (((int)DateTime.Now.DayOfWeek) != weekId)
{
    Response.Write("<h4>Sorry! Today is NOT the week you selected. Please use Back button of browser and try again!</h4>");
    Response.End();
}

简化解决方案:

int weekId = -1;
//first try and parse the int from the query string. Note the  ?? will execute the statement 
//following when the result is null. https://msdn.microsoft.com/en-us/library/ms173224.aspx
//the test the day of week parsed
if (!int.TryParse((Request.QueryString["WeekId"] ?? ""), out weekId) || ((int)DateTime.Now.DayOfWeek) != weekId)
{
    Response.Write("<h4>Sorry! Today is NOT the week you selected. Please use Back button of browser and try again!</h4>");
    Response.End();
}
相关问题