【问题标题】:Else, If statement breaking否则,如果语句中断
【发布时间】:2016-03-14 12:24:59
【问题描述】:

第一个 if 语句有效,但是当您尝试执行 else 语句时,它会中断:int SearchUser = Convert.ToInt32(SearchResult.SelectedRow.Cells[1].Text);

protected void SearchResult_SelectedIndexChanged(object sender, EventArgs e)
{

    // open new connection
    SqlConnection connection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    connection1.Open();

    int SearchUser = Convert.ToInt32(SearchResult.SelectedRow.Cells[1].Text);
    int Student = Convert.ToInt32(Session["UserID"]);


    if (SearchResult.SelectedRow.Cells[1].Text != null)
    {

    String PT = "INSERT into PTutors(PersonalTutorID, StudentID) values(@SearchUser, @Student)";
    SqlCommand myCommand = new SqlCommand(PT, connection1);
    myCommand.Parameters.AddWithValue("@SearchUser", SearchUser);
    myCommand.Parameters.AddWithValue("@Student", Student);
    myCommand.ExecuteNonQuery();


    Response.Write("<script type='text/javascript'>");
    Response.Write("alert('Student Assigned to Personal Tutor');");
    Response.Write("document.location.href='ViewAssignedTutors.aspx';");
    Response.Write("</script>");
    }

    else 
    { 

    int SearchAPM = Convert.ToInt32(SearchResult.SelectedRow.Cells[9].Text);

        String PT2 = "INSERT into PTutors(PersonalTutorID, StudentID) values(@SearchAPM, @Student)";
        SqlCommand myCommand = new SqlCommand(PT2, connection1);
        myCommand.Parameters.AddWithValue("@SearchAPM", SearchAPM);
        myCommand.Parameters.AddWithValue("@Student", Student);
        myCommand.ExecuteNonQuery();


        Response.Write("<script type='text/javascript'>");
        Response.Write("alert('Student Assigned to Personal Tutor');");
        Response.Write("document.location.href='ViewAssignedTutors.aspx';");
        Response.Write("</script>");
    }
}

【问题讨论】:

  • 异常信息是什么?你说它坏了,但不是错误是什么?我来猜一猜 - SearchResult.SelectedRow.Cells[9].Text 是否包含可以转换为整数的值?
  • 错误消息是:在 mscorlib.dll 中发生了“System.FormatException”类型的异常,但未在用户代码中处理其他信息:输入字符串的格式不正确。
  • 您要转换为整数的文本字段的值是多少?
  • 我认为这行可能会抛出错误SearchResult.SelectedRow.Cells[1].Text
  • @LishaMcNally 您需要确保SearchResult.SelectedRow.Cells[9] 实际上包含一个可以转换为整数的值。如果它不是数字,没有人能用魔法消除它。

标签: c# asp.net session if-statement sql-update


【解决方案1】:

尝试而不是简洁

  int SearchAPM = Convert.ToInt32(SearchResult.SelectedRow.Cells[9].Text);

健谈

  int SearchAPM = 0;

  if (!int.TryParse(SearchResult.SelectedRow.Cells[9].Text, out SearchAPM)) {
    Message.Show(String.Format("\"{0}\" is not an integer value.", 
      SearchResult.SelectedRow.Cells[9].Text));
  }

那么,请看一下弹出的消息并调试程序;

【讨论】:

    【解决方案2】:

    您尝试转换的数据似乎格式不正确。如Msdn中所述

    尝试在转换上放置一个断点,并查看您从单元格中提供了哪些值。另外,您是如何确定应该使用字段 9 的?您确定不只是选择了错误的字段吗?

    或者,您也可以将您的转化者封装在 try catch 中

    try{
        int SearchAPM = Convert.ToInt32(SearchResult.SelectedRow.Cells[9].Text);
    } catch(FormatException ex)
    {
       // Do something to debug/handle
       SearchApm = -1;
       Trace.WriteLine("Oh no, {0} is not correctly formated",SearchResult.SelectedRow.Cells[9].Text);
    }
    

    或者甚至简化这种 try catch 的版本可能是 try parse,它将在内部处理错误,如果失败,则返回 0。

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 2014-07-26
      • 2015-02-03
      • 1970-01-01
      相关资源
      最近更新 更多