Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
namespace BusinessLogicLayer
{
    class BusinessClass
    {
        public string _Name;
        public int _Roll_number;
        public string _Email_Id;
        public string _Mobile_Number;

        DataAccessClass objdal = new DataAccessClass();

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        public int Roll_Number
        {
            get
            {
                return _Roll_number;
            }
            set
            {
                _Roll_number = value;
            }
        }
        public string Email_Id
        {
            get
            {
                return _Email_Id;
            }
            set
            {
                _Email_Id = value;
            }
        }
        public string Mobile_Number
        {
            get
            {
                return _Mobile_Number;
            }
            set
            {
                _Mobile_Number = value;
            }

        }

in dataaccess layer the code is

C#
namespace DataAccessLayer
{
    public class DataAccessClass
    {
        //String cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection con;
        SqlCommand cmd;
        public DataAccessClass()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("sp_InsertEmpdetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name",objdal.Name);//here i could not access the objdal object why?please help me
            cmd.Parameters.AddWithValue("@Roll_Number", objdal.Roll_Number);//
            cmd.Parameters.AddWithValue("@Email_Id", objdal.Email_Id);
            cmd.Parameters.AddWithValue("@Mobile_Number", objdal.Mobile_Number);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

 }

and what code should i write to insert data into database
Posted
Updated 3-Mar-14 22:22pm
v2
Comments
Ankur\m/ 4-Mar-14 4:23am    
Business layer should contain your business rules. They may change and you just need to change it there and not at multiple places. For example, in a banking application, you have a saving a/c minimum balance amount. That validation should be in your business layer.
What is it that you actually want. Your question is not very clear.

1 solution

Answers to you questions:
Firstly, the code you should write in your business layer should be 'as the name suggests' any business operation related logic like performing any calculations, business validations etc.

Regarding your second question, i think you are already inserting records into your database with a stored procedure as seen from your code. So I am not sure about that as your question is confusing.

Regarding your comment //here i could not access the objdal object why?please help me
I think you want to access the properties defined in your business class. If that is the case, make your 'BusinessClass' class as public and then create an instance of it. And then use it in your data access layer.


However there are a few observations from my side.
1. In the business layer, since you are already having public properties, you should mark the variables as private. In your case they are public.

2. It seems you are naming your stored procedures starting with 'sp_'. This should be avoided since system stored procedures too are named as 'sp_' hence this is not a best practice from SQL Server point of view since it will have a slight performance overhead. You can read it more by googling it out. :)

3. It is always advisable to use try catch blocks for preventing any un-handled exceptions breaking your application. In your case, you can use try catch block in your data access layer to log any exceptions and also to close any connections.

I hope this might help you...
 
Share this answer
 
Comments
raxhemanth 5-Mar-14 0:02am    
thakyou hitesh

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900