Hello All,
I have created a .aspx page with 7 text boxes and a save button. On click event following code is fired. It save the property information to sql server and also generate a unique Id for that porperty and fatch that id and show it on that screen. I have created a .cs file for TrustConnectionString as below.
Can someone please help me or guide me, is this a good practice to write code? If not can you please help me how to break this large code into classes or method and call them one by one.
Thanks for your help.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class Data_Entry___Update_Property : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["id"] != null )
{
lblEditProp.Text = Request.QueryString["id"];
string tid = lblEditProp.Text;
Label1.Text = tid;
Session["Tid"] = tid;
}
if (Session["Tid"] != null)
{
lblEditProp.Text = Session["Tid"].ToString();
string tid = lblEditProp.Text;
Label1.Text = tid;
Session["Tid"] = tid;
}
btnUpdate.Enabled = false;
if (lblEditProp.Text != "" )
{
TrustConnectionString tConnString = new TrustConnectionString();
SqlConnection conn = new SqlConnection(tConnString.ConString);
SqlCommand comm = new SqlCommand("EditProperty", conn);
comm.CommandType = CommandType.StoredProcedure;
SqlParameter paratrustid = new SqlParameter("@trustid", SqlDbType.VarChar);
paratrustid.Value = lblEditProp.Text;
comm.Parameters.Add(paratrustid);
SqlParameter paralocationcode = new SqlParameter("@locationcode", SqlDbType.VarChar);
paralocationcode.Value = int.Parse(Session["Locationid"].ToString());
comm.Parameters.Add(paralocationcode);
SqlParameter paraLocationID = new SqlParameter("@locationid", SqlDbType.VarChar);
paraLocationID.Value = Session["Location"].ToString();
comm.Parameters.Add(paraLocationID);
try
{
conn.Open();
SqlDataReader sdredit = comm.ExecuteReader();
while (sdredit.Read())
{
TtridentSale.Text = sdredit[0].ToString();
TOffer.Text = sdredit[1].ToString();
TAddress.Text = sdredit[2].ToString();
TCity.Text = sdredit[3].ToString();
TState.Text = sdredit[4].ToString();
TZip.Text = sdredit[5].ToString();
TAccountNumber.Text = sdredit[6].ToString();
btnSave.Enabled = false;
btnUpdate.Enabled = true;
}
}
catch (SqlException seq)
{
Label7.Text = seq.Message;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
TrustConnectionString tConnString = new TrustConnectionString();
SqlConnection conn = new SqlConnection(tConnString.ConString);
SqlCommand comm = new SqlCommand("insertPropertyInformation_test", conn);
comm.CommandType = CommandType.StoredProcedure;
SqlParameter para1 = new SqlParameter("@Tsale", SqlDbType.VarChar);
para1.Value = TtridentSale.Text;
comm.Parameters.Add(para1);
SqlParameter para2 = new SqlParameter("@Offer", SqlDbType.VarChar);
para2.Value = TOffer.Text;
comm.Parameters.Add(para2);
SqlParameter para3 = new SqlParameter("@propaddress", SqlDbType.VarChar);
para3.Value = TAddress.Text;
comm.Parameters.Add(para3);
SqlParameter para4 = new SqlParameter("@city", SqlDbType.VarChar);
para4.Value = TCity.Text;
comm.Parameters.Add(para4);
SqlParameter para5 = new SqlParameter("@state", SqlDbType.VarChar);
para5.Value = TState.Text.ToUpper();
comm.Parameters.Add(para5);
SqlParameter para6 = new SqlParameter("@zipcode", SqlDbType.VarChar);
para6.Value = TZip.Text;
comm.Parameters.Add(para6);
SqlParameter para7 = new SqlParameter("@locationcode", SqlDbType.VarChar);
para7.Value = int.Parse(Session["Locationid"].ToString());
comm.Parameters.Add(para7);
SqlParameter para8 = new SqlParameter("@locationid", SqlDbType.VarChar);
para8.Value = Session["Location"].ToString();
comm.Parameters.Add(para8);
SqlParameter parax = new SqlParameter("@tid", SqlDbType.Int);
parax.Direction = ParameterDirection.Output;
comm.Parameters.Add(parax);
try
{
conn.Open();
SqlDataReader sdr = comm.ExecuteReader();
Label2.Text = comm.Parameters["@tid"].Value.ToString();
string tidadd = Label2.Text;
Session["Tidadd"] = tidadd; //Session For TrustID
}
catch (SqlException seq)
{
Label7.Text = seq.Message;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
string str1 = WebConfigurationManager.ConnectionStrings["Trust_Conn"].ConnectionString;
SqlConnection conn1 = new SqlConnection(str1);
SqlCommand comm1 = new SqlCommand("GetDepositeAccountNumber", conn1);
comm1.CommandType = CommandType.StoredProcedure;
SqlParameter para9 = new SqlParameter("@Tsale", SqlDbType.VarChar);
para9.Value = TtridentSale.Text;
comm1.Parameters.Add(para9);
SqlParameter para11 = new SqlParameter("@propaddress", SqlDbType.VarChar);
para11.Value = TAddress.Text;
comm1.Parameters.Add(para11);
SqlParameter para12 = new SqlParameter("@city", SqlDbType.VarChar);
para12.Value = TCity.Text;
comm1.Parameters.Add(para12);
SqlParameter para13 = new SqlParameter("@state", SqlDbType.VarChar);
para13.Value = TState.Text.ToUpper();
comm1.Parameters.Add(para13);
SqlParameter para14 = new SqlParameter("@zipcode", SqlDbType.VarChar);
para14.Value = TZip.Text;
comm1.Parameters.Add(para14);
try
{
conn1.Open();
SqlDataReader sdr1 = comm1.ExecuteReader();
while (sdr1.Read())
{
TAccountNumber.Text = sdr1[0].ToString();
}
conn1.Close();
}
catch (SqlException sqe)
{
Label7.Text = sqe.Message;
}
finally
{
if (conn1.State == ConnectionState.Open)
{
conn1.Close();
}
}
}