【发布时间】: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