【问题标题】:Return value not changing in business logic layer返回值在业务逻辑层不变
【发布时间】:2014-08-05 05:54:03
【问题描述】:

我必须在我的应用程序中使用三层架构。我必须在业务访问层中获取数据访问层返回值,但我无法在业务访问层中获取不断变化的值。

我的代码隐藏页面

  protected void btnRegister_Click(object sender, EventArgs e)
            {
                if (IsPostBack)
                {
                    obj.empReg(txtUserName.Text, txtPassword.Text, isactive, Convert.ToInt32(hdntest.Value));

                    Response.Redirect("~/Login.aspx");
                }                               

            }

我的业务访问层:-

 public int empReg(string username, string password, int isactive, int returncode)
        {
           return obj.EmpRegistration(username, password, isactive, returncode);

        }

我的数据访问层:-

 public int EmpRegistration(string username, string password, int isactive, int returncode)
        {
            isactive = 1;
            string cs = ConfigurationManager.ConnectionStrings["EmployeeDB"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(cs))
            {
                SqlCommand com = new SqlCommand("sp_RegisterationUser", connection);
                com.CommandType = CommandType.StoredProcedure;

                string encryptedpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
                SqlParameter paramusername = new SqlParameter("@Username", username);
                com.Parameters.Add(paramusername);
                SqlParameter parampassword = new SqlParameter("@Password", encryptedpassword);
                com.Parameters.Add(parampassword);
                SqlParameter paramisactive = new SqlParameter("@isactive", isactive);
                com.Parameters.Add(paramisactive);
                connection.Open();               
                returncode = (int)com.ExecuteScalar();

                return returncode;
            }

        }

这里一切正常,但returncode 参数值在我的业务层中没有改变。我不知道如何在我的业务访问层中获取returncode 参数。

【问题讨论】:

  • 这里的returncode参数在哪里?
  • 您正在从后面的代码中调用obj.empReg(),并且您从该方法返回一个整数而不使用它!?那么为什么在不使用它的时候返回一个整数呢?
  • 您的EmpRegistration 方法是否有异常?

标签: c# asp.net


【解决方案1】:

您没有在任何地方获取/分配返回值。可能您正在寻找类似的东西,

int Result = obj.empReg(txtUserName.Text, txtPassword.Text, isactive, Convert.ToInt32(hdntest.Value));

【讨论】:

    【解决方案2】:

    可能你需要做以下改动,不用的时候不需要在参数中发送返回码。

    代码背后

     protected void btnRegister_Click(object sender, EventArgs e)
     { 
         //...      
         int iReturnCode = obj.empReg(txtUserName.Text, txtPassword.Text, isactive);
    
         //if(iReturnCode > 0)
         Response.Redirect("~/Login.aspx");                                        
     }
    

    业务层

     public int empReg(string username, string password, int isactive)
     {
           return obj.EmpRegistration(username, password, isactive);
     }
    

    数据访问层

    public int EmpRegistration(string username, string password, int isactive)
    { 
       try
       {
         //...
         using (SqlConnection connection = new SqlConnection(cs))
         {
              //...            
              return Convert.ToInt32(com.ExecuteScalar());                
         }
       }
       catch{ return -1; }       
     }
    

    【讨论】:

      猜你喜欢
      • 2010-12-18
      • 1970-01-01
      • 2011-12-03
      • 2011-11-26
      • 2017-04-29
      • 2016-08-12
      • 2014-09-04
      • 2013-02-18
      • 1970-01-01
      相关资源
      最近更新 更多