Help Writing Codes in Class.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hshah
    New Member
    • Mar 2007
    • 10

    Help Writing Codes in Class.

    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 TrustConnection String 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.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using System.Data.Sql Client;
    using System.Web.Conf iguration;

    public partial class Data_Entry___Up date_Property : System.Web.UI.P age
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {



    if (!IsPostBack)
    {

    if (Request.QueryS tring["id"] != null )
    {
    lblEditProp.Tex t = Request.QuerySt ring["id"];
    string tid = lblEditProp.Tex t;
    Label1.Text = tid;
    Session["Tid"] = tid;
    }
    if (Session["Tid"] != null)
    {
    lblEditProp.Tex t = Session["Tid"].ToString();
    string tid = lblEditProp.Tex t;
    Label1.Text = tid;
    Session["Tid"] = tid;
    }


    btnUpdate.Enabl ed = false;



    if (lblEditProp.Te xt != "" )
    {

    TrustConnection String tConnString = new TrustConnection String();

    SqlConnection conn = new SqlConnection(t ConnString.ConS tring);

    SqlCommand comm = new SqlCommand("Edi tProperty", conn);

    comm.CommandTyp e = CommandType.Sto redProcedure;


    SqlParameter paratrustid = new SqlParameter("@ trustid", SqlDbType.VarCh ar);
    paratrustid.Val ue = lblEditProp.Tex t;
    comm.Parameters .Add(paratrusti d);


    SqlParameter paralocationcod e = new SqlParameter("@ locationcode", SqlDbType.VarCh ar);
    paralocationcod e.Value = int.Parse(Sessi on["Locationid "].ToString());
    comm.Parameters .Add(paralocati oncode);


    SqlParameter paraLocationID = new SqlParameter("@ locationid", SqlDbType.VarCh ar);
    paraLocationID. Value = Session["Location"].ToString();
    comm.Parameters .Add(paraLocati onID);


    try
    {
    conn.Open();
    SqlDataReader sdredit = comm.ExecuteRea der();

    while (sdredit.Read() )
    {
    TtridentSale.Te xt = 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.Enabl ed = true;

    }
    }

    catch (SqlException seq)
    {
    Label7.Text = seq.Message;
    }

    finally
    {
    if (conn.State == ConnectionState .Open)
    {

    conn.Close();
    }
    }


    }

    }
    }

    protected void btnSave_Click(o bject sender, EventArgs e)
    {

    TrustConnection String tConnString = new TrustConnection String();

    SqlConnection conn = new SqlConnection(t ConnString.ConS tring);
    SqlCommand comm = new SqlCommand("ins ertPropertyInfo rmation_test", conn);

    comm.CommandTyp e = CommandType.Sto redProcedure;

    SqlParameter para1 = new SqlParameter("@ Tsale", SqlDbType.VarCh ar);
    para1.Value = TtridentSale.Te xt;
    comm.Parameters .Add(para1);

    SqlParameter para2 = new SqlParameter("@ Offer", SqlDbType.VarCh ar);
    para2.Value = TOffer.Text;
    comm.Parameters .Add(para2);

    SqlParameter para3 = new SqlParameter("@ propaddress", SqlDbType.VarCh ar);
    para3.Value = TAddress.Text;
    comm.Parameters .Add(para3);

    SqlParameter para4 = new SqlParameter("@ city", SqlDbType.VarCh ar);
    para4.Value = TCity.Text;
    comm.Parameters .Add(para4);

    SqlParameter para5 = new SqlParameter("@ state", SqlDbType.VarCh ar);
    para5.Value = TState.Text.ToU pper();
    comm.Parameters .Add(para5);

    SqlParameter para6 = new SqlParameter("@ zipcode", SqlDbType.VarCh ar);
    para6.Value = TZip.Text;
    comm.Parameters .Add(para6);


    SqlParameter para7 = new SqlParameter("@ locationcode", SqlDbType.VarCh ar);
    para7.Value = int.Parse(Sessi on["Locationid "].ToString());
    comm.Parameters .Add(para7);


    SqlParameter para8 = new SqlParameter("@ locationid", SqlDbType.VarCh ar);
    para8.Value = Session["Location"].ToString();
    comm.Parameters .Add(para8);

    SqlParameter parax = new SqlParameter("@ tid", SqlDbType.Int);
    parax.Direction = ParameterDirect ion.Output;
    comm.Parameters .Add(parax);




    try
    {
    conn.Open();
    SqlDataReader sdr = comm.ExecuteRea der();
    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 = WebConfiguratio nManager.Connec tionStrings["Trust_Conn "].ConnectionStri ng;

    SqlConnection conn1 = new SqlConnection(s tr1);
    SqlCommand comm1 = new SqlCommand("Get DepositeAccount Number", conn1);
    comm1.CommandTy pe = CommandType.Sto redProcedure;

    SqlParameter para9 = new SqlParameter("@ Tsale", SqlDbType.VarCh ar);
    para9.Value = TtridentSale.Te xt;
    comm1.Parameter s.Add(para9);

    SqlParameter para11 = new SqlParameter("@ propaddress", SqlDbType.VarCh ar);
    para11.Value = TAddress.Text;
    comm1.Parameter s.Add(para11);

    SqlParameter para12 = new SqlParameter("@ city", SqlDbType.VarCh ar);
    para12.Value = TCity.Text;
    comm1.Parameter s.Add(para12);

    SqlParameter para13 = new SqlParameter("@ state", SqlDbType.VarCh ar);
    para13.Value = TState.Text.ToU pper();
    comm1.Parameter s.Add(para13);

    SqlParameter para14 = new SqlParameter("@ zipcode", SqlDbType.VarCh ar);
    para14.Value = TZip.Text;
    comm1.Parameter s.Add(para14);

    try
    {
    conn1.Open();
    SqlDataReader sdr1 = comm1.ExecuteRe ader();
    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();



    }

    }




    }
Working...