在写ASP.NET登录页面 的时候,登录不成功。
 user.UserName = tbName.Text.Trim(); 这里已经填了,像user.UserName="daaa".但是 UserDao里面userName值 为空。
自己调试过,发现 class UserDao里面的user.UserName,与其他相应的值为空。我在想是不函数调用问题?
需要写反射吗??namespace ArticleCMS.Entity
{
    public class User
    {
        private int userId;        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        private string userName;        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string userPassword;        public string UserPassword
        {
            get { return userPassword; }
            set { userPassword = value; }
        }
        private int userPower;        public int UserPower
        {
            get { return userPower; }
            set { userPower = value; }
        }
    }
}namespace ArticleCMS.Dao
{
    public class UserDao
    {
  /// <summary>
        ///检查用户是否存在 
        /// </summary>
        /// <returns>如果存在返回100,否则返回-100</returns>
        public int UserExist()
        {
            SqlParameter[] para = new SqlParameter[3];
            para[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 20);
            para[0].Value = user.UserName;
            para[1] = new SqlParameter("@UserPassword", SqlDbType.VarChar, 20);
            para[1].Value = user.UserPassword;
            para[2] = new SqlParameter("@ReturnValue", SqlDbType.Int);
            para[2].Direction = ParameterDirection.ReturnValue;            datastore.Updata("UserExists", para);
            return (int)para[2].Value;        }
namespace ArticleCMS
{
    public partial class Login : DB.ValidateBase
    {
        Dao.UserDao userdao = new ArticleCMS.Dao.UserDao();
        Entity.User user = new ArticleCMS.Entity.User();        protected void Page_Load(object sender, EventArgs e)
        {
            
        }        protected void LoginSubmit_Click(object sender, EventArgs e)
        {            user.UserName = tbName.Text.Trim();
            user.UserPassword = tbPassword.Text.Trim();
            if (tbRegister.Text == base.strValidate)
           {
              int isExist= userdao.UserExist();
              if (isExist == 100)
              {                     SqlDataReader dr = userdao.UserIDPower();
                  Session["UserId"] = dr["UserId"].ToString();
                  Session["UserPower"] = dr["UserPower"].ToString();
                  Response.Redirect("http://www.baidu.com");
              }
              else
              {
                  Response.Write("<script>alert('您的登录有误,请核对后再重新登录!');location='javascript:history.go(-1)';</script>");
              }
           }
           else
           {
             Response.Write("<script>alert('请正确输入验证码!');location='javascript:history.go(-1)';</script>");
           }
           
        }
    }
}

解决方案 »

  1.   

    设置断点,单步跟踪查看数据
    执行UserExists看看效果
      

  2.   

    过程或函数 'UserExists' 需要参数 '@UserName',但未提供该参数。这个是设置断点后显示的。我知道出现这个实现的原因是我在public class UserDao 写了 Entity.User user = new ArticleCMS.Entity.User();在 public partial class Login : DB.ValidateBase 也写了 Entity.User user = new ArticleCMS.Entity.User();因为这样的造成的,在不同的命名空间,创建类的实例。不过就是不知道应该如何的解决。???
    是否要用什么函数调用 呢??
      

  3.   

      datastore.Updata("UserExists", para); 没有写出来是用存储过程调用数据库查询然后返回
     public int Updata(string proc, SqlParameter[] para)
            {
                if (conn != null)
                {
                    conn.Close();
                }
                conn.Open();
                SqlCommand cmd = Command(proc, para);
                int nI;
                try
                {
                   nI = cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    cmd.Dispose();
                    conn.Close();
                }
                return nI;
            }
      public SqlCommand Command(string proc, SqlParameter[] para)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = proc;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;            if (para != null)
                {
                    foreach (SqlParameter pa in para)
                    {
                        cmd.Parameters.Add(pa);
                    }
                }
                return cmd;
            }
      

  4.   

    检查参数是否都添加protected static void PrepareCommand(SqlConnection conn, SqlCommand cmd, string strSqlOrProcedure, CommandType cmdType) {
                cmd.Connection = conn;
                cmd.CommandText = strSqlOrProcedure;
                cmd.CommandType = cmdType;
                if (Params != null && Values != null && Params.Length == Values.Length) {
                    for (int i = 0; i < Params.Length; i++) {
                        SqlParameter sp = new SqlParameter();
                        sp.ParameterName = Params[i];
                        sp.Value = Values[i];
                        sp.Direction = ParameterDirection.Input;
                        cmd.Parameters.Add(sp);
                    }
                }
                Params = null;
                Values = null;
            }
      

  5.   

    感觉你的代码应该是对的,你可以在调试的时候看下tbName.Text.Trim()这个有没有值?
      

  6.   

    楼主调试代码,看下那值有没有传到实体类和你执行的SQL
      

  7.   


     public int UserExist(User user)
            {
                SqlParameter[] para = new SqlParameter[3];
                para[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 20);
                para[0].Value = user.UserName;
                para[1] = new SqlParameter("@UserPassword", SqlDbType.VarChar, 20);
                para[1].Value = user.UserPassword;
                para[2] = new SqlParameter("@ReturnValue", SqlDbType.Int);
                para[2].Direction = ParameterDirection.ReturnValue;            datastore.Updata("UserExists", para);
                return (int)para[2].Value;        }
    int isExist= userdao.UserExist();
      

  8.   


    int isExist= userdao.UserExist(user);