在从 sql server 获取 asp.net 中的记录时,它总是返回 null..here's code- 即使记录在数据库中可用,对象 oid 在运行时也会有 null。请帮忙。谢谢。
protected void btnLogin_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["DBMS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand com = new SqlCommand("select id from reg where user=@user and password=@pass", con);
com.Parameters.AddWithValue("@user", txUName.Text);
com.Parameters.AddWithValue("@pass", txPass.Text);
con.Open();
var oid = com.ExecuteScalar();
con.Close();
if (oid == null)
{
lblEr.Visible = true;
}
else
{
Session.Add("id", oid.ToString());
Session.Add("user", txUName.Text);
com = new SqlCommand("select type from dbo.reg where user=@u", con);
com.Parameters.AddWithValue("@u", txUName.Text);
con.Open();
var otype = com.ExecuteScalar();
con.Close();
var vtype = "admin";
if (vtype == otype.ToString())
{
Session.Add("admin", otype.ToString());
Response.Redirect("admin.aspx");
}
Response.Redirect("user.aspx");
}
}