L
labjac
Guest
Hallo
Again just want to say thanks for all the assistance this far, seems like we are getting there and start making some sense.
I want to start moving onto the Exception handling in the DataLayer, and also how the best way to reply to incorrect data in text boxes. Let's start with the text boxes first.
We collect all the information in the form a create a object from the BO which is passed to the BA layer, then we set a ErrorCode = 0 at the beginning of the validation method, it will "fall" throught the routine and if any of the values is incorrect it will set ErrorCode to a number which we can then use in the form to display some Message.. eg. ErrorCode = 1 "Please enter Company Name", ErrorCode = 2 "Please enter address" ........ Is there a better way to handle this, and secondly if this is OK is there a way to create a lookup that we can already populate the TextMessages without typing it all in the Class? (Similar to SQL PrimKey and Description. Then Point on form to the message corresponding to the returned ErrorCode?
The BA Class and two methods.
public class CompanyInfoBA
{
public Connection db = new Connection();
// here declare the queries and db operations needed for the application
public int SaveNewCompany(CompanyInfoBO objCompanyInfo)
{
CompanyInfoDA ComInfo = new CompanyInfoDA();
//validation will be done before createing the object to be send to DAL
int ErrorCode = 0;
ErrorCode = textvalidation(objCompanyInfo);
if (ErrorCode == 0)
{
ErrorCode = ComInfo.InsertCompanyData(objCompanyInfo);
}
return ErrorCode;
}
public int textvalidation(CompanyInfoBO Textfield)
{
int ErrorCode = 0;
if (Textfield.CompanyName.Length == 0)
{
ErrorCode = 1;
}
if (Textfield.Address1.Length == 0)
{
ErrorCode = 2;
}
if (Textfield.Address2.Length == 0)
{
ErrorCode = 3;
}
return ErrorCode;
}
}
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)
{
CompanyInfoBA comInfoBA = new CompanyInfoBA();
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 = comInfoBA.SaveNewCompany(ComInfo);
if (success == 0)
{
MessageBox.Show("All good");
}
else
{
MessageBox.Show("Something is wrong ErrorCode:" + success);
}
}
}
Secondly, I hear about a Global Exception handler, using Google there seems to be a lot of information in the matter but with different opinions, What is the best way to get the Exception code back to the userform. (Understand the Try Catch should be with the Caller.
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;
}
}
Need some try catches when trying to retrieve data from the above to the Datalayer, but how to reply back to the caller, the method only got one int return a value which at this stage is being use just to give a number.
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...
Again just want to say thanks for all the assistance this far, seems like we are getting there and start making some sense.
I want to start moving onto the Exception handling in the DataLayer, and also how the best way to reply to incorrect data in text boxes. Let's start with the text boxes first.
We collect all the information in the form a create a object from the BO which is passed to the BA layer, then we set a ErrorCode = 0 at the beginning of the validation method, it will "fall" throught the routine and if any of the values is incorrect it will set ErrorCode to a number which we can then use in the form to display some Message.. eg. ErrorCode = 1 "Please enter Company Name", ErrorCode = 2 "Please enter address" ........ Is there a better way to handle this, and secondly if this is OK is there a way to create a lookup that we can already populate the TextMessages without typing it all in the Class? (Similar to SQL PrimKey and Description. Then Point on form to the message corresponding to the returned ErrorCode?
The BA Class and two methods.
public class CompanyInfoBA
{
public Connection db = new Connection();
// here declare the queries and db operations needed for the application
public int SaveNewCompany(CompanyInfoBO objCompanyInfo)
{
CompanyInfoDA ComInfo = new CompanyInfoDA();
//validation will be done before createing the object to be send to DAL
int ErrorCode = 0;
ErrorCode = textvalidation(objCompanyInfo);
if (ErrorCode == 0)
{
ErrorCode = ComInfo.InsertCompanyData(objCompanyInfo);
}
return ErrorCode;
}
public int textvalidation(CompanyInfoBO Textfield)
{
int ErrorCode = 0;
if (Textfield.CompanyName.Length == 0)
{
ErrorCode = 1;
}
if (Textfield.Address1.Length == 0)
{
ErrorCode = 2;
}
if (Textfield.Address2.Length == 0)
{
ErrorCode = 3;
}
return ErrorCode;
}
}
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)
{
CompanyInfoBA comInfoBA = new CompanyInfoBA();
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 = comInfoBA.SaveNewCompany(ComInfo);
if (success == 0)
{
MessageBox.Show("All good");
}
else
{
MessageBox.Show("Something is wrong ErrorCode:" + success);
}
}
}
Secondly, I hear about a Global Exception handler, using Google there seems to be a lot of information in the matter but with different opinions, What is the best way to get the Exception code back to the userform. (Understand the Try Catch should be with the Caller.
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;
}
}
Need some try catches when trying to retrieve data from the above to the Datalayer, but how to reply back to the caller, the method only got one int return a value which at this stage is being use just to give a number.
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...