none
Passing object to method using Data and object layers RRS feed

  • Question

  • Hallo

    Thanks for the assistance this far, but I think I got a bit of a information overload, and cannot get my head around the passing of the object from the Bussiness to the Data layer... There is still some part missing so pleae don't be to critical on the error handling etc.. Still trying to understand the structure.

    The solution structure below, this is for insert, append and delete records for a Companies. what will be added is the same structure for the rest of the information. eg. FurnaceInfoBA, FurnaceInfoBO, FurnaceInfoDA etc... till we got all the structures for the 20 different user forms.. or at least this is what I understand needs to happen.. 

    At this moment I'm just trying to save the data from the userform to SQL.. (But got trouble passing the object variable between the methods...

    Customer userform..

    using WCS_Cap.BOL;
    using WCS_Cap.BAL;
    
    namespace WCS_Cap.PLL
    {
        public partial class FrCustomers : Form
        {
            CompanyInfoBO ComInfo = new CompanyInfoBO();
    
            public FrCustomers()
            {
                InitializeComponent();
            }
    
            private void btExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void btnSave_Click(object sender, EventArgs e)
            {
                int success = 0;
    
                ComInfo.CompanyName = txtCustomerName.Text;
                ComInfo.Address1 = txtAddress1.Text;
                ComInfo.Address2 = txtAddress2.Text;
                ComInfo.City = txtCity.Text;
                ComInfo.Region = txtRegion.Text;
                ComInfo.PostalCode = txtPostalCode.Text;
                ComInfo.PhoneNumber = txtPhoneNumber.Text;
    
                success = CompanyInfoBA.SaveNewCompany(ComInfo);                        
            }
        }
    }
    

    Object variables:

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WCS_Cap.BOL
    {
        class CompanyInfoBO
        {
            // here we declare the variables used in the application
            public int CompanyID { get; set; }
            public string CompanyName { get; set; }
            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string City { get; set; }
            public string Region { get; set; }
            public string PostalCode { get; set; }
            public string PhoneNumber { get; set; }
            public string Email { get; set; }
        }
    }
    

    Business Logic Layer

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using WCS_Cap.BOL;
    using WCS_Cap.DAL;
    
    namespace WCS_Cap.BAL
    {
        class CompanyInfoBA
        {
            public Connection db = new Connection();
            // here declare the queries and db operations needed for the application
    
    
            public int SaveNewCompany(CompanyInfoBO objCompanyInfo)
            {
                //validation will be done before creating the object to be send to DAL                  
                int ErrorCode = 0;
    
                ErrorCode = CompanyInfoDA.InsertCompanyData(objCompanyInfo);
    
                return ErrorCode;
            }
    
        }
    }
    

    DAL:

    using System.Data;
    using System.Data.SqlClient;
    using WCS_Cap.BOL;
    
    
    namespace WCS_Cap.DAL
    {
        public class CompanyInfoDA
        {
            DataTable dt = new DataTable();
            Connection db = new Connection();
           // CompanyInfoBO info = new CompanyInfoBO();
            
            
    
            public int InsertCompanyData(CompanyInfoBO info)
            {          
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO tbCompanyMainDetails VALUES('" + info.CompanyName + "','" + info.Address1 + "'";
                return db.ExeNonQuery(cmd);            
            }
    
            public object SelectAllCompanyData()
            {
                Connection con = new Connection();
    
    
    
                return dt; 
    
            }
    
        }

    Connection to db...

    using System.Data;
    using System.Data.SqlClient;
    
    namespace WCS_Cap.DAL
    {
        public class Connection
        {
           public SqlConnection conn = new SqlConnection("Data Source=WIN10_SSD\\PULSESQLEXPRESS;Initial Catalog=WCSData;Persist Security Info=True;User ID=sa;Password=*****");
            
            public SqlConnection getcon()
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                return conn;
            }
    
            public int ExeNonQuery(SqlCommand cmd)  //Insert Delete and Append to SQL database
            {
                cmd.Connection = getcon();
                int rowaffected = -1;
                conn.Close();
                return rowaffected;
            }
    
            public object ExeScalar(SqlCommand cmd) //To Retrieve a Single value from DB or Query
            {
                cmd.Connection = getcon();
                object obj = -1;
                obj = cmd.ExecuteScalar();
                conn.Close();
                return obj;
            }
    
            public DataTable ExeReader (SqlCommand cmd)  // Exe Reader to Perform Select Query
            {
                cmd.Connection = getcon();
                SqlDataReader sdr;
                DataTable dt = new DataTable();
    
                sdr = cmd.ExecuteReader();
                dt.Load(sdr);
                return dt;
            }


    labjac

    Tuesday, October 16, 2018 1:28 PM

Answers

  • Hi labjac,

    Visual Studio has a really nasty habit of automatically making a class private when you add a new class to a project (no modifier means it's private):

    class MyClass
    {
    }

    It didn't used to do that, it used to make it public (many, many years ago ... not sure which version of VS started defaulting to no modifier). So, you have to always remember to add the public modifier to it (usually you're going to want your classes to be public).

    public class MyClass
    {
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by labjac Tuesday, October 16, 2018 3:53 PM
    Tuesday, October 16, 2018 3:13 PM
    Moderator

All replies

  • Can you provide the error you are getting.

    I think you are  not able to pass the BO object because you declared the class as internal.

    The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace.

    ----------------

    If this answers your question, please mark it as answered

    Tuesday, October 16, 2018 1:50 PM
  •   public class CompanyInfoDA
        {
            DataTable dt = new DataTable();
            Connection db = new Connection();
           // CompanyInfoBO info = new CompanyInfoBO();
            
            
    
            public int InsertCompanyData(CompanyInfoBO info)
            {          
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO tbCompanyMainDetails VALUES('" + info.CompanyName + "','" + info.Address1 + "'";
                return db.ExeNonQuery(cmd);            
            }
    
            public object SelectAllCompanyData()
            {
                Connection con = new Connection();
    
    
    
                return dt; 
    
            }
    
        }
    public int InsertCompanyData(CompanyInfoBO info) (Inconsistant 


    labjac

    Tuesday, October 16, 2018 2:24 PM
  • Hallo

    As I said, might be something simple, but what I don't understand is why I can define the method with CompanyInfoBO as a parameters in the BO class, but doing exactly the same thing in the CompanyDA class will not accept the object as a parameter?

       public class CompanyInfoDA
        {
            DataTable dt = new DataTable();
            Connection db = new Connection();
           // CompanyInfoBO info = new CompanyInfoBO();
            
            
    
            public int InsertCompanyData(CompanyInfoBO info) // Not happy
            {          
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO tbCompanyMainDetails VALUES('" + info.CompanyName + "','" + info.Address1 + "'";
                return db.ExeNonQuery(cmd);            
            }
    
            public object SelectAllCompanyData()
            {
                Connection con = new Connection();
    
    
    
                return dt; 
    
            }
    
        }
    }
       class CompanyInfoBA
        {
            public Connection db = new Connection();
            // here declare the queries and db operations needed for the application
    
    
            public int SaveNewCompany(CompanyInfoBO objCompanyInfo) //Happy ??
            {
                //validation will be done before createing the object to be send to DAL                  
                int ErrorCode = 0;
    
                ErrorCode = CompanyInfoDA.InsertCompanyData(objCompanyInfo);
    
                return ErrorCode;
            }
    
        }



    labjac

    Tuesday, October 16, 2018 2:33 PM
  • Apologies.

    My mistake, thanks forgot to declare a instance of the DA Class to be able to access the method.

    Thank you..


    labjac

    Tuesday, October 16, 2018 2:41 PM
  • Hi labjac,

    Visual Studio has a really nasty habit of automatically making a class private when you add a new class to a project (no modifier means it's private):

    class MyClass
    {
    }

    It didn't used to do that, it used to make it public (many, many years ago ... not sure which version of VS started defaulting to no modifier). So, you have to always remember to add the public modifier to it (usually you're going to want your classes to be public).

    public class MyClass
    {
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by labjac Tuesday, October 16, 2018 3:53 PM
    Tuesday, October 16, 2018 3:13 PM
    Moderator
  • Thanks Bonnie

    Yes, that was part of my problem, I've been staring at the screen for the last week trying to make sense of the 3 tair architecture and slowly but surely loosing my mind and the basic of the syntax.. 

    But on the right track again and seems to be passing the object correctly now with some minor issues like error handling and using parameters for the SQL insert the we should be OK.

    Thanks again all for always helping even if it's simple questions.

    Regards,

    labjac, 


    labjac

    Tuesday, October 16, 2018 3:53 PM
  • It looks like you're beginning a new project.  It may benefit you to use entity framework as your data layer and a LInq repository for your business layer.
    Tuesday, October 16, 2018 4:06 PM
  • Hallo

    Thanks, this seems to be a good option, I've been looking at this last night, for now I want to try and understand how everything fits together, will then move onto the Entity framework as you suggested.

    Regards,


    labjac

    Wednesday, October 17, 2018 5:45 AM
  • Hi labjac,

    Keeping it simple when you're first starting out is a good idea, I think. Lots of developers use Entity Framework and swear that it's the "greatest thing since sliced bread"   ;0) … but I don't, I prefer "rolling my own" DAL because it gives me more control over what I'm doing.

    Having a separate DAL, as you do, is definitely the right way to go (in fact, it's a "best practice" kind of architecture). That way, if you decide to move to Entity Framework or any other way of doing your Data Access, you simply put new stuff in your DAL, and the rest of the application should not be affected at all.  The rest of the application should not care how it gets its data, it just knows that it comes from the DAL.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, October 17, 2018 2:04 PM
    Moderator