I have code that executes on a button click. It connects to a sql database and reads two values. All I want to achieve is to place this code in a separate class called 'DataManager' and then from my button click call a method in this class and get the two strings into my textboxes.
string sReference = txtReference.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_SELECT_CONSHEAD_BY_ENQUIRY_NUMBER";
cmd.Parameters.AddWithValue("@EnquiryNumber", sReference);
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
txtAccount.Text = sdr["Consignee"].ToString();
txtAccount_Printed.Text = sdr["Consignee_Printed"].ToString();
}
con.Close();
con.Dispose();
I was thinking my method should look something like this
// Select from ConsHead by Reference Number
public string SelectConsHead(string sReference, out string sAccount, out string sAccount_Printed)
{
sAccount_Printed = "";
sAccount = "";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_SELECT_CONSHEAD_BY_ENQUIRY_NUMBER";
cmd.Parameters.AddWithValue("@EnquiryNumber", sReference);
// Attach connection to command
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
sAccount = sdr["Consignee"].ToString();
sAccount_Printed = sdr["Consignee_Printed"].ToString();
}
con.Close();
con.Dispose();
return sAccount + sAccount_Printed;
}
but I'm not sure on how to call the method and retrieve the corresponding values into the textboxes.