The perfect way is to do the things is the proper way. you need to segregate the things accordingly.
for that first make a class file which will have all your Data access related function. For example
public static DataTable GetTable(SqlCommand cmd)
{
DataTable tbl = null;
SqlConnection conn = null;
try
{
conn =
cmd.Connection = conn;
tbl = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
tbl.Load(dr);
dr.Close();
dr.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn != null)
{
conn.Close();
}
}
return tbl;
}
public static DataRow SampleStoreProcCall(Int32 prama1,Int32 Param2, String Prama3)
{
DataRow row = null;
DataTable tbl = null;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Sb_SampleStoreProc";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@prama1", prama1);
cmd.Parameters.AddWithValue("@prama2", Param4);
cmd.Parameters.AddWithValue("@prama3", Param3);
tbl = DataAccess.GetTable(cmd);
row = tbl.Rows[0];
return row;
}
DataRow row = YourClassFile.SampleStoreProcCall(Callprama1,CallParam2, CallPrama3);
txtbox1.txt = row["column1"].ToString();
txtbox2.txt = row["column2"].ToString();
txtbox3.txt = row["column3"].ToString();
Hope this will help.