Im a beginner to .net and could you guide me to right direction. My problem is based on the following code. Here I have 4 variations of same method and all 4 variations are working fine. But I just want to know what is the best or standard way of doing this?
Code explanation.
From a windows form Im calling 'viewAccount()' method which is in 'bankAccount' class. Its purpose is to get relivant bank account details of a employee from the data base and then those details should be shown in the text boxes of calling form.
Also please note that I have reduced no of line to make it more readable. Appreciate your any help towards the right direction.
Thank you.
Example 01 - Method will return a 'bankAccount' abject with fields populated with data from the data base
class bankAccount
{
string acNo;
string acName;
string bank;
string acType;
frmShowAccount form=new frmShowAccount();
public bankAccount viewAccount( string acNo )
{
this.acNo = acNo;
using (SqlConnection newCon = new SqlConnection(db.GetConnectionString))
{
SqlCommand newCmd = new SqlCommand("SELECT Employee.Name, BankAccount.ac_name, BankAccount.bank_name, BankAccount.ac_type FROM BankAccount INNER JOIN Employee ON BankAccount.emp_id = Employee.Emp_ID WHERE (BankAccount.ac_no = @bankAccount)", newCon);
newCmd.Parameters.Add("@bankAccount", SqlDbType.Char).Value = acNo;
newCon.Open();
SqlDataReader rdr = newCmd.ExecuteReader();
rdr.Read();
form.txtEmpName.text = rdr.GetString(0);
this.acName = rdr.GetString(1);
this.bank = rdr.GetString(2);
this.acType = rdr.GetString(3);
return this;
}
}
}
********
//CALLING THE ABOVE METHOD...
bankAccount newBA=new bankAccount();
newBA=newBA.viewAccount(txtACNo.text);
txtACName.text=newBA.acName;
Example 02 - Method will return a data reader and it will be used by the form to get data
class bankAccount
{
string acNo;
string acName;
string bank;
string acType;
public SqlDataReader viewAccount( string acNo )
{
this.acNo = acNo;
using (SqlConnection newCon = new SqlConnection(db.GetConnectionString))
{
SqlCommand newCmd = new SqlCommand("Same SELECT …",newCon);
newCmd.Parameters.Add()…
newCon.Open();
SqlDataReader rdr = newCmd.ExecuteReader();
rdr.Read();
return rdr;
}
}
}
*************
//CALLING THE ABOVE METHOD...
bankAccount newBA=new bankAccount();
SqlDataReader rdr=newBA.viewAccount(txtACNo.text)
txtACName.text=rdr.getString(1);
Example 03 - This method want return values but explicitly assign values to the text boxes in the form
class bankAccount
{
string acNo;
string acName;
string bank;
string acType;
frmShowAccount form=new frmShowAccount();
public void viewAccount( string acNo )
{
this.acNo = acNo;
using (SqlConnection newCon = new SqlConnection(db.GetConnectionString))
{
SqlCommand newCmd = new SqlCommand("Same SELECT …", newCon);
newCmd.Parameters.Add()…
newCon.Open();
SqlDataReader rdr = newCmd.ExecuteReader();
rdr.Read();
form.txtName.text=rdr[0];
form.txtACName.text=rdr[1];
form.txtBankName.text=rdr[2];
form.txtACType.text=rdr[3];
}
}
}
************
//CALLING THE ABOVE METHOD
bankAccount newBA=new bankAccount();
newBA.form.this;
Example 04 - This method want return any value. It will only initialize instance fields of the class with the data
class bankAccount
{
string acNo;
string acName;
string bank;
string acType;
frmShowAccount form=new frmShowAccount();
public void viewAccount( string acNo )
{
this.acNo = acNo;
using (SqlConnection newCon = new SqlConnection(db.GetConnectionString))
{
SqlCommand newCmd = new SqlCommand("Same SELECT …)", newCon);
newCmd.Parameters.Add()…
newCon.Open();
SqlDataReader rdr = newCmd.ExecuteReader();
rdr.Read();
form.txtName.text=rdr[0];
this.acName=rdr[1];
this.bank=rdr[2];
this.acType=rdr[3];
}
}
***************
CALLING THE ABOVE METHOD
bankAccount newBA=new bankAccount();
txtACName.text=newBA.acName;