制作用户登录验证时遇到的问题.
错误提示:运算符“==”无法应用于“object”和“int”类型的操作数.
具体代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;namespace network
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbx_uid;
protected System.Web.UI.WebControls.TextBox tbx_password;
protected System.Web.UI.WebControls.Label lbl_message;
protected System.Web.UI.WebControls.Button btn_dl;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
private void btn_dl_Click(object sender,System.EventArgs e)
{
string strconn = ConfigurationSettings.AppSettings["mdb"];
OleDbConnection cn = new OleDbConnection(strconn);
cn.Open();
string mysql = "select * from [admin] where uid='"+tbx_uid.Text+"'and password='"+tbx_password.Text+"'";
OleDbCommand cm = new OleDbCommand(mysql,cn);
OleDbDataReader dr = cm.ExecuteReader();
if(dr.Read())
{
lbl_message.Text="";
Session["uid"]=dr["uid"];
Session["power"]=dr["power"];
if(Session["power"] == 1)
{
Response.Redirect("admin.aspx");
}
else
{
Response.Redirect("guanli.aspx");
}
}
else
{
lbl_message.Text = "Sorry,your name/password not right!";
}

cn.Close();
}

解决方案 »

  1.   


    Session["uid"]=dr["uid"];
    Session["power"]=dr["power"];
    if(Session["power"] == 1)
    {
    Response.Redirect("admin.aspx");
    }
    else
    {
    Response.Redirect("guanli.aspx");
    }
    改成
    Session["uid"]=dr["uid"].ToString();
    Session["power"]=dr["power"].ToString();
    if(Session["power"] == "1")
    {
    Response.Redirect("admin.aspx");
    }
    else
    {
    Response.Redirect("guanli.aspx");
    }
      

  2.   

    新的错误提示:可能非有意的引用比较;若要获取值比较,请将左边转换为类型“string”
      

  3.   

    int应该可以,object建议你用equal方法来进行
      

  4.   

    Session读出来的是object类型,无法和int类型进行比较。应做相应类型转换才行。
      

  5.   

    if(Session["power"].equals( "1"))