【发布时间】:2014-08-30 06:52:07
【问题描述】:
我正在开发一个简单的 asp.net 登录页面。一个简单的代码,当用户登录时,它会使用 sql 进行验证,并直接进入工作人员或管理员的页面。但我有这个错误 (int.Parse(myReader.ToString()) > 0) ...输入字符串的格式不正确 我的代码..
string Connection = "Data Source=(ip);Initial Catalog=..;Persist Security Info=True;User ID=(id);Password=(pass)";
SqlConnection myConn = new SqlConnection(Connection);
SqlCommand SelectCommand = new SqlCommand("select * from tar_login where Username ='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' and Position='" + ddlPosition.Text + "';", myConn);
myConn.Open();
var myReader = SelectCommand.ExecuteScalar();
myConn.Close();
if (myReader != null)
{
if (int.Parse(myReader.ToString()) > 0)
{
if (ddlPosition.Text == "Admin")
{
Response.Redirect("manager.aspx");
}
}
}
if (myReader != null)
{
if (int.Parse(myReader.ToString()) > 0)
{
if (ddlPosition.Text == "Staff")
{
Response.Redirect("staff.aspx");
}
}
}
else
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "Incorrect. Please try again";
myConn.Close();
}
.help 作为 asp.net 和 c# 的新手
【问题讨论】:
-
这意味着
myReader.ToString()不是数字,或者不是 int 格式。 -
调试你的代码,在
var myReader = SelectCommand.ExecuteScalar();添加一个断点并检查它的值是什么,可能不是整数或空 -
@Noctis 在断点处我得到 myReader 为空