using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DBAccess;
public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (returnName(UserName.Value.Trim()))
            {
                if (Password.Value.Trim() == returnPassword(Password.Value.Trim()))
                {
                    returnSession(UserName.Value.Trim());
                    Response.Redirect("default.aspx");
                }
                 else
                    {
                        UseJavaScript.ExcuteAlert("密码错误!");
                    }
            }
            else
            {
                UseJavaScript.ExcuteAlert("该工号不存在!");
            }
        }
    }
    private bool returnName(string LoginName)
    {
        DbHelper db = new DbHelper();
        string sql;
        int count = 0;
        try
        {
            sql = " select count(*) from T_User where  Name=@Name";
            DbCommand cmd = db.GetSqlStringCommond(sql);
            db.AddInParameter(cmd, "@Name", DbType.String, LoginName);
            count = Convert.ToInt32(db.ExecuteScalar(cmd));
        }
        catch (Exception e)
        {
            throw new ApplicationException(e.Message);
        }
        if (count == 0)
            return false;
        else
            return true;
    }
    private string returnPassword(string LoginName)
    {
        DbHelper db = new DbHelper();
        string password = "";
        DataSet ds;
        string sql;
        try
        {
            sql = " select password from t_user where  Name=@Name";
            DbCommand cmd = db.GetSqlStringCommond(sql);
            db.AddInParameter(cmd, "@Name", DbType.String, LoginName);
            ds = db.ExecuteDataSet(cmd);
            if (ds.Tables[0].Rows.Count > 0)
            {
                password = ds.Tables[0].Rows[0]["password"].ToString();
            }
        }
        catch (Exception e)
        {
            throw new ApplicationException(e.Message);
        }
        return password;    }
    private void returnSession(string LoginName)
    {
        DbHelper db = new DbHelper();
        DataSet ds;
        string sql;
        try
        {
            sql = " select id,name from t_user where  Name=@Name";
            DbCommand cmd = db.GetSqlStringCommond(sql);
            db.AddInParameter(cmd, "@Name", DbType.String, LoginName);
            ds = db.ExecuteDataSet(cmd);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Session["userid"] = ds.Tables[0].Rows[0]["id"].ToString();
                Session["name"] = ds.Tables[0].Rows[0]["name"].ToString();
            }
        }
        catch (Exception e)
        {
            throw new ApplicationException(e.Message);
        }    }
  
}

解决方案 »

  1.   

    sql = " select count(*) from T_User where  Name=@Name"; 
            Dbconnection conn= new   Dbconnection(链接数据库字符串)
                DbCommand cmd = db.GetSqlStringCommond(sql); 
                db.AddInParameter(cmd, "@Name", DbType.String, LoginName); 
              conn.open
    count =cmd.exectuenonreader();
                count = Convert.ToInt32(db.ExecuteScalar(cmd)); 
      

  2.   

    if (Page.IsPostBack) 
    这个是判断非第一次加载页面,确信要这样实现吗?此外,你想找什么错?说明一下
      

  3.   

    调试一下吧。他是在哪个地方出错。单单看你的代码很难看出来。
    你还有封装了一个DbHelper 类。。