第一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'是否包含可以变成整数的值? – user1666620
错误消息是:mscorlib.dll中发生类型'System.FormatException'的异常,但未在用户代码中处理 附加信息:输入字符串格式不正确。 –
你试图变成一个整数的文本字段的值是什么? – user1666620