Passing object to method using Data and object layers

  • Thread starter Thread starter labjac
  • Start date Start date
L

labjac

Guest
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...

1348399.jpg


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

Continue reading...
 
Back
Top