第一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