我在Page_Load方法中的Try Catch遇到了问题。如果会话为空,则捕获应该生效,但在Visual Studio中按下"Continue“之前,它不会生效。但之后一切都会按其应有的方式运行。你知道问题出在哪里吗?
下面是我的代码:
SqlConnection conn = new SqlConnection(@"Data Source=localhost\sqlexpress;Initial Catalog=VulnerableApp;Integrated Security=True");
try
{
conn.Open();
SqlCommand validation = new SqlCommand();
validation.Connection = conn;
validation.CommandText = "Select Role From Users Where Username ='" + Session["Username"] + "';";
string rank = (string)validation.ExecuteScalar();
if (rank.Trim() == "Admin" || rank.Trim() == "Moderator")
{
lblAdmin.Text = "Welcome " + (string)Session["Username"];
}
else
{
MessageBox.Show("Your rank is to low " + rank);
Response.Redirect("Done.aspx");
}conn.Close();
}
catch (NullReferenceException)
{
MessageBox.Show("You have to be logged in");
Response.Redirect("Login.aspx");
}发布于 2020-04-20 21:09:34
尝尝这个
using (SqlConnection conn = new SqlConnection(@"Data Source=localhost\sqlexpress;Initial Catalog=VulnerableApp;Integrated Security=True"))
{
try
{
conn.Open();
SqlCommand validation = new SqlCommand();
validation.Connection = conn;
validation.CommandText = "Select Role From Users Where Username ='" + Session["Username"] + "';";
string rank = (string)validation.ExecuteScalar();
if (rank.Trim() == "Admin" || rank.Trim() == "Moderator")
{
lblAdmin.Text = "Welcome " + (string)Session["Username"];
}
else
{
MessageBox.Show("Your rank is to low " + rank);
Response.Redirect("Done.aspx");
}
}
catch (Exception ex)
{
MessageBox.Show("You have to be logged in" + ex.Message);
Response.Redirect("Login.aspx");
}
}https://stackoverflow.com/questions/61323196
复制相似问题