ADO Approach Business logic layer and data access layer
How to make Bll class in Asp.net/MVC?
Right click on the namespace(Project name) -> Add-> New Folder after making folder right click on the folder ->Add->class and give any name to the class than write a method with name Bllregistration with return type void and Model as a parameter.
write the following code in defined Bllregistration method
public void Bllregistration(Mdlsignup mdl)
{
DALAccount obj = new DALAccount(); // DAL class object creation
obj.DalRegisterUser(mdl); // call to DAL method with model object as a parameter
}
we have to create an object of DAL class here because we have to access Dal method here and pass model object as a parameter
How to make DAL Class in Asp.net/MVC?
Right click on the namespace(Project name) -> Add-> New Folder after making folder right click on the folder ->Add->class and give any name to the class than write a method with name DalRegisterUser with return type void and Model as a parameter and write the following code in DAL class
private string _ConnString;
public DALAccount() //its constructor method with the class name to get connection string from Web.config file
{
_ConnString = ConfigurationManager.ConnectionStrings[“SqlConnection”].ConnectionString;
}
public void DalRegisterUser(Mdlsignup mdl) //DAL method to map model parameter values with stored procedure parameter
{
SqlParameter[] parm = new SqlParameter[7];
parm[0] = new SqlParameter(“@fname”, SqlDbType.NVarChar)
{
Value = mdl.Fname
};
parm[1] = new SqlParameter(“@lname”, SqlDbType.NVarChar)
{
Value = mdl.Lname
};
parm[2] = new SqlParameter(“@Email”, SqlDbType.NVarChar)
{
Value = mdl.Email
};
parm[3] = new SqlParameter(“@password”, SqlDbType.NVarChar)
{
Value = mdl.Password
};
parm[4] = new SqlParameter(“@city”, SqlDbType.NVarChar)
{
Value = mdl.City
};
parm[5] = new SqlParameter(“@country”, SqlDbType.NVarChar)
{
Value = mdl.Country
};
parm[6] = new SqlParameter(“@phone”, SqlDbType.NVarChar)
{
Value = mdl.Cell
};
SqlHelper.SaveData(this._ConnString, CommandType.StoredProcedure, “spw-usersignup”, parm);
//SaveData of SqlHelper class will called here and we will pass the following four to that method
//connection string, command type that is storedProcedure, stored procedure name, and mapped parameter
}
public static int SaveData(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandparameters)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = commandType;
cmd.CommandText = commandText;
for (int i = 0; i < commandparameters.Length; i++)
{
cmd.Parameters.Add(commandparameters[i]);
}
cmd.Connection = con;
return cmd.ExecuteNonQuery();
}
}
1 thought on “ADO Approach Business logic layer and data access layer”
Leave a Reply
You must be logged in to post a comment.
Great
But one thing we can declare a constant variable nd first map procedure parameter with constant value and then these constant variable should mapped in sqlparameter value array.