由于页面加载,更新查询无法正常工作

时间:2014-05-28 08:34:20

标签: c# asp.net sql sql-update pageload

目前正在一个项目中,用户页面将在页面加载中加载,更新用户详细信息,更新数据未反映在我的数据库中,页面再次加载旧数据..

这是我的代码:

public partial class userprofile : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }

        SqlConnection con = new SqlConnection(constr);
        string strquery = "select * from insertuser Where companyname='Prhemnath'";
        SqlCommand cmdd = new SqlCommand();
        cmdd.CommandType = CommandType.Text;
        cmdd.CommandText = strquery;
        cmdd.Connection = con;
        try
        {
            con.Open();
            SqlDataReader sdr = cmdd.ExecuteReader();
            while (sdr.Read())
            {
                txtcompanyname.Text = sdr["companyname"].ToString();
                ddtypeofcmpy.SelectedValue = sdr["typeofcompany"].ToString();
                txtauthname.Text = sdr["authorizedperson"].ToString();
                txtauthdesg.Text = sdr["authorizedperdesig"].ToString();
                txtregoffice.Text = sdr["registeredoffaddress"].ToString();
                txtcorroffice.Text = sdr["correspondanceaddress"].ToString();
                txtll1.Text = sdr["landline1"].ToString();
                txtll2.Text = sdr["landline2"].ToString();
                txtmob1.Text = sdr["mobile1"].ToString();
                txtmob2.Text = sdr["mobile2"].ToString();
                txtfax.Text = sdr["email"].ToString();
                txtemail.Text = sdr["email"].ToString();
                txtweb.Text = sdr["website"].ToString();
                txtusername.Text = sdr["username"].ToString();
                txtnumempcp.Text = sdr["empwithcp"].ToString();
                txtnonumempcp.Text = sdr["empwocp"].ToString();
                txtchnpan.Text = sdr["newCHAPAN"].ToString();
                txtchalicence.Text = sdr["newCHAlicence"].ToString();
                txtchptreg.Text = sdr["CHPTregnumber"].ToString();
                txtchptvalid.Text = sdr["CHPTvalidity"].ToString();
                chkaurangabad.Checked = Convert.ToBoolean(sdr["aurangabad"]);
                chkbangalore.Checked = Convert.ToBoolean(sdr["bangalore"]);
                chkchn.Checked = Convert.ToBoolean(sdr["chennai"]);
                chklcochin.Checked = Convert.ToBoolean(sdr["cochin"]);
                chkdelhi.Checked = Convert.ToBoolean(sdr["delhi"]);
                chkgoa.Checked = Convert.ToBoolean(sdr["goa"]);
                chkhyd.Checked = Convert.ToBoolean(sdr["hyderabad"]);
                chkindore.Checked = Convert.ToBoolean(sdr["kakinada"]);
                chkkandla.Checked = Convert.ToBoolean(sdr["kolkatta"]);
                chkludhiana.Checked = Convert.ToBoolean(sdr["ludhiana"]);
                chkmangalore.Checked = Convert.ToBoolean(sdr["mangalore"]);
                chkmumbai.Checked = Convert.ToBoolean(sdr["bombay"]);
                chknagpur.Checked = Convert.ToBoolean(sdr["nagpur"]);
                chknashik.Checked = Convert.ToBoolean(sdr["nashik"]);
                chkpatna.Checked = Convert.ToBoolean(sdr["patna"]);
                chkpune.Checked = Convert.ToBoolean(sdr["pune"]);
                chkrajasthan.Checked = Convert.ToBoolean(sdr["rajasthan"]);
                chkthiruvanthapuram.Checked = Convert.ToBoolean(sdr["trivandram"]);
                chktuticorin.Checked = Convert.ToBoolean(sdr["tutucorin"]);
                chkvisakhapatanam.Checked = Convert.ToBoolean(sdr["vishkapattinam"]);
                chkconsolidation.Checked = Convert.ToBoolean(sdr["consolidation"]);
                chkCHA.Checked = Convert.ToBoolean(sdr["CHBA"]);
                cnkFF.Checked = Convert.ToBoolean(sdr["FF"]);
                chkhandling.Checked = Convert.ToBoolean(sdr["handlingliveani"]);
                chkPE.Checked = Convert.ToBoolean(sdr["handlingPA"]);
                chkIATA.Checked = Convert.ToBoolean(sdr["IATAagents"]);
                chkerecplant.Checked = Convert.ToBoolean(sdr["erectionofplant"]);
                chktransp.Checked = Convert.ToBoolean(sdr["liquidtrans"]);
                chkMTO.Checked = Convert.ToBoolean(sdr["MTOagents"]);
                chkODC.Checked = Convert.ToBoolean(sdr["ODCtran"]);
                chkImport.Checked = Convert.ToBoolean(sdr["projimport"]);
                chkhandequip.Checked = Convert.ToBoolean(sdr["supplycranes"]);
                chkbulkcargo.Checked = Convert.ToBoolean(sdr["bulkcargo"]);
                chkcargo.Checked = Convert.ToBoolean(sdr["containercargo"]);
                chkware.Checked = Convert.ToBoolean(sdr["warehousing"]);
            }
        }
        finally
        {
            con.Close();
            con.Dispose();
        }

    }
    protected void btnconfirm_Click(object sender, EventArgs e)
    {
        string companyname = txtcompanyname.Text;
        string typeofcompany = ddtypeofcmpy.SelectedValue;
        string authorizedperson = txtauthname.Text;
        string authorizedperdesig = txtauthdesg.Text;
        string registeredoffaddress = txtregoffice.Text;
        string correspondanceaddress = txtcorroffice.Text;
        string landline1 = txtll1.Text;
        string landline2 = txtll2.Text;
        string mobile1 = txtmob1.Text;
        string mobile2 = txtmob2.Text;
        string fax = txtfax.Text;
        string email = txtemail.Text;
        string website = txtweb.Text;
        string username = txtusername.Text;

        string empwithcp = txtnumempcp.Text;
        string empwocp = txtnonumempcp.Text;
        string oldCHAno = txtoldcha1.Text + "" + txtoldcha2.Text;
        string newCHAPAN = txtchnpan.Text;
        string newCHAlicence = txtchalicence.Text;
        string CHPTregnumber = txtchptreg.Text;
        string CHPTvalidity = txtchptvalid.Text;
        string aurangabad = chkaurangabad.Checked ? "1" : "0";
        string bangalore = chkbangalore.Checked ? "1" : "0";
        string chennai = chkchn.Checked ? "1" : "0";
        string cochin = chklcochin.Checked ? "1" : "0";
        string delhi = chkdelhi.Checked ? "1" : "0";
        string goa = chkgoa.Checked ? "1" : "0";
        string hyderabad = chkhyd.Checked ? "1" : "0";
        string indore = chkindore.Checked ? "1" : "0";
        string kakinada = chkkakinada.Checked ? "1" : "0";
        string kandla = chkkandla.Checked ? "1" : "0";
        string kolkatta = chkkolkotta.Checked ? "1" : "0";
        string ludhiana = chkludhiana.Checked ? "1" : "0";
        string mangalore = chkmangalore.Checked ? "1" : "0";
        string mumbai = chkmumbai.Checked ? "1" : "0";
        string nagpur = chknagpur.Checked ? "1" : "0";
        string nashik = chknashik.Checked ? "1" : "0";
        string patna = chkpatna.Checked ? "1" : "0";
        string pune = chkpune.Checked ? "1" : "0";
        string rajasthan = chkrajasthan.Checked ? "1" : "0";
        string trivandram = chkrajasthan.Checked ? "1" : "0";
        string tutucorin = chktuticorin.Checked ? "1" : "0";
        string vishkapattinam = chkvisakhapatanam.Checked ? "1" : "0";
        string consolidation = chkconsolidation.Checked ? "1" : "0";
        string CHBA = chkCHA.Checked ? "1" : "0";
        string FF = cnkFF.Checked ? "1" : "0";
        string handlingliveani = chkhandling.Checked ? "1" : "0";
        string handlingPA = chkPE.Checked ? "1" : "0";
        string IATAagents = chkIATA.Checked ? "1" : "0";
        string erectionofplant = chkerecplant.Checked ? "1" : "0";
        string liquidtrans = chktransp.Checked ? "1" : "0";
        string MTOagents = chkMTO.Checked ? "1" : "0";
        string ODCtran = chkODC.Checked ? "1" : "0";
        string projimport = chkImport.Checked ? "1" : "0";
        string supplycranes = chkhandequip.Checked ? "1" : "0";
        string bulkcargo = chkbulkcargo.Checked ? "1" : "0";
        string containercargo = chkcargo.Checked ? "1" : "0";
        string warehousing = chkware.Checked ? "1" : "0";

        using (SqlConnection conupdate = new SqlConnection(constr))
        {
            string queryupdate = "update insertuser set typeofcompany=@typeofcompany,authorizedperson=@authorizedperson,authorizedperdesig=@authorizedperdesig,registeredoffaddress=@registeredoffaddress,correspondanceaddress=@correspondanceaddress,landline1=@landline1,landline2=@landline2,mobile1=@mobile1,mobile2=@mobile2,fax=@fax,email=@email,website=@website,username=@username,empwithcp=@empwithcp,empwocp=@empwocp,oldCHAno=@oldCHAno,newCHAPAN=@newCHAPAN,newCHAlicence=@newCHAlicence,CHPTregnumber=@CHPTregnumber,CHPTvalidity=@CHPTvalidity,aurangabad=@aurangabad,bangalore=@bangalore,chennai=@chennai,cochin=@cochin,delhi=@delhi,goa=@goa,hyderabad=@hyderabad,indore=@indore,kakinada=@kakinada,kandla=@kandla,kolkatta=@kolkatta,ludhiana=@ludhiana,mangalore=@mangalore,bombay=@bombay,nagpur=@nagpur,nashik=@nashik,patna=@patna,pune=@pune,rajasthan=@rajasthan,trivandram=@trivandram,tutucorin=@tutucorin,vishkapattinam=@vishkapattinam,consolidation=@consolidation,CHBA=@CHBA,FF=@FF,handlingliveani=@handlingliveani,handlingPA=@handlingPA,IATAagents=@IATAagents,erectionofplant=@erectionofplant,liquidtrans=@liquidtrans,MTOagents=@MTOagents,ODCtran=@ODCtran,projimport=@projimport,supplycranes=@supplycranes,bulkcargo=@bulkcargo,containercargo=@containercargo,warehousing=@warehousing where companyname='David'";
            using (SqlCommand cmd = new SqlCommand(queryupdate, conupdate))
            {

                cmd.Parameters.AddWithValue("@typeofcompany", typeofcompany);
                cmd.Parameters.AddWithValue("@authorizedperson", authorizedperson);
                cmd.Parameters.AddWithValue("@authorizedperdesig", authorizedperdesig);
                cmd.Parameters.AddWithValue("@registeredoffaddress", registeredoffaddress);
                cmd.Parameters.AddWithValue("@correspondanceaddress", correspondanceaddress);
                cmd.Parameters.AddWithValue("@landline1", landline1);
                cmd.Parameters.AddWithValue("@landline2", landline2);
                cmd.Parameters.AddWithValue("@mobile1", mobile1);
                cmd.Parameters.AddWithValue("@mobile2", mobile2);
                cmd.Parameters.AddWithValue("@fax", fax);
                cmd.Parameters.AddWithValue("@email", email);
                cmd.Parameters.AddWithValue("@website", website);
                cmd.Parameters.AddWithValue("@username", username);

                cmd.Parameters.AddWithValue("@empwithcp", empwithcp);
                cmd.Parameters.AddWithValue("@empwocp", empwocp);
                cmd.Parameters.AddWithValue("@oldCHAno", oldCHAno);
                cmd.Parameters.AddWithValue("@newCHAPAN", newCHAPAN);
                cmd.Parameters.AddWithValue("@newCHAlicence", newCHAlicence);
                cmd.Parameters.AddWithValue("@CHPTregnumber", CHPTregnumber);
                cmd.Parameters.AddWithValue("@CHPTvalidity", CHPTvalidity);
                cmd.Parameters.AddWithValue("@aurangabad", Convert.ToInt32(aurangabad));
                cmd.Parameters.AddWithValue("@bangalore", Convert.ToInt32(bangalore));
                cmd.Parameters.AddWithValue("@chennai", Convert.ToInt32(chennai));
                cmd.Parameters.AddWithValue("@cochin", Convert.ToInt32(cochin));
                cmd.Parameters.AddWithValue("@delhi", Convert.ToInt32(delhi));
                cmd.Parameters.AddWithValue("@goa", Convert.ToInt32(goa));
                cmd.Parameters.AddWithValue("@hyderabad", Convert.ToInt32(hyderabad));
                cmd.Parameters.AddWithValue("@indore", Convert.ToInt32(indore));
                cmd.Parameters.AddWithValue("@kakinada", Convert.ToInt32(kakinada));
                cmd.Parameters.AddWithValue("@kandla", Convert.ToInt32(kandla));
                cmd.Parameters.AddWithValue("@kolkatta", Convert.ToInt32(kolkatta));
                cmd.Parameters.AddWithValue("@ludhiana", Convert.ToInt32(ludhiana));
                cmd.Parameters.AddWithValue("@mangalore", Convert.ToInt32(mangalore));
                cmd.Parameters.AddWithValue("@bombay", Convert.ToInt32(mumbai));
                cmd.Parameters.AddWithValue("@nagpur", Convert.ToInt32(nagpur));
                cmd.Parameters.AddWithValue("@nashik", Convert.ToInt32(nashik));
                cmd.Parameters.AddWithValue("@patna", Convert.ToInt32(patna));
                cmd.Parameters.AddWithValue("@pune", Convert.ToInt32(pune));
                cmd.Parameters.AddWithValue("@rajasthan", Convert.ToInt32(rajasthan));
                cmd.Parameters.AddWithValue("@trivandram", Convert.ToInt32(trivandram));
                cmd.Parameters.AddWithValue("@tutucorin", Convert.ToInt32(tutucorin));
                cmd.Parameters.AddWithValue("@vishkapattinam", Convert.ToInt32(vishkapattinam));
                cmd.Parameters.AddWithValue("@consolidation", Convert.ToInt32(consolidation));
                cmd.Parameters.AddWithValue("@CHBA", Convert.ToInt32(CHBA));
                cmd.Parameters.AddWithValue("@FF", Convert.ToInt32(FF));
                cmd.Parameters.AddWithValue("@handlingliveani", Convert.ToInt32(handlingliveani));
                cmd.Parameters.AddWithValue("@handlingPA", Convert.ToInt32(handlingPA));
                cmd.Parameters.AddWithValue("@IATAagents", Convert.ToInt32(IATAagents));
                cmd.Parameters.AddWithValue("@erectionofplant", Convert.ToInt32(erectionofplant));
                cmd.Parameters.AddWithValue("@liquidtrans", Convert.ToInt32(liquidtrans));
                cmd.Parameters.AddWithValue("@MTOagents", Convert.ToInt32(MTOagents));
                cmd.Parameters.AddWithValue("@ODCtran", Convert.ToInt32(ODCtran));
                cmd.Parameters.AddWithValue("@projimport", Convert.ToInt32(projimport));
                cmd.Parameters.AddWithValue("@supplycranes", Convert.ToInt32(supplycranes));
                cmd.Parameters.AddWithValue("@bulkcargo", Convert.ToInt32(bulkcargo));
                cmd.Parameters.AddWithValue("@containercargo", Convert.ToInt32(containercargo));
                cmd.Parameters.AddWithValue("@warehousing", Convert.ToInt32(warehousing));
                cmd.Connection = conupdate;
                conupdate.Open();
                cmd.ExecuteNonQuery();
                conupdate.Close();
            }
        }

    }

} }

1 个答案:

答案 0 :(得分:0)

您需要在

中编写页面加载代码
if (!IsPostBack)
{

}

块。

它会起作用。
page_load的代码应该如下所示

        if (!IsPostBack)
        {

        SqlConnection con = new SqlConnection(constr);
        string strquery = "select * from insertuser Where companyname='Prhemnath'";
        SqlCommand cmdd = new SqlCommand();
        cmdd.CommandType = CommandType.Text;
        cmdd.CommandText = strquery;
        cmdd.Connection = con;
        try
        {
            con.Open();
            SqlDataReader sdr = cmdd.ExecuteReader();
            while (sdr.Read())
            {
                txtcompanyname.Text = sdr["companyname"].ToString();
                ddtypeofcmpy.SelectedValue = sdr["typeofcompany"].ToString();
                txtauthname.Text = sdr["authorizedperson"].ToString();
                txtauthdesg.Text = sdr["authorizedperdesig"].ToString();
                txtregoffice.Text = sdr["registeredoffaddress"].ToString();
                txtcorroffice.Text = sdr["correspondanceaddress"].ToString();
                txtll1.Text = sdr["landline1"].ToString();
                txtll2.Text = sdr["landline2"].ToString();
                txtmob1.Text = sdr["mobile1"].ToString();
                txtmob2.Text = sdr["mobile2"].ToString();
                txtfax.Text = sdr["email"].ToString();
                txtemail.Text = sdr["email"].ToString();
                txtweb.Text = sdr["website"].ToString();
                txtusername.Text = sdr["username"].ToString();
                txtnumempcp.Text = sdr["empwithcp"].ToString();
                txtnonumempcp.Text = sdr["empwocp"].ToString();
                txtchnpan.Text = sdr["newCHAPAN"].ToString();
                txtchalicence.Text = sdr["newCHAlicence"].ToString();
                txtchptreg.Text = sdr["CHPTregnumber"].ToString();
                txtchptvalid.Text = sdr["CHPTvalidity"].ToString();
                chkaurangabad.Checked = Convert.ToBoolean(sdr["aurangabad"]);
                chkbangalore.Checked = Convert.ToBoolean(sdr["bangalore"]);
                chkchn.Checked = Convert.ToBoolean(sdr["chennai"]);
                chklcochin.Checked = Convert.ToBoolean(sdr["cochin"]);
                chkdelhi.Checked = Convert.ToBoolean(sdr["delhi"]);
                chkgoa.Checked = Convert.ToBoolean(sdr["goa"]);
                chkhyd.Checked = Convert.ToBoolean(sdr["hyderabad"]);
                chkindore.Checked = Convert.ToBoolean(sdr["kakinada"]);
                chkkandla.Checked = Convert.ToBoolean(sdr["kolkatta"]);
                chkludhiana.Checked = Convert.ToBoolean(sdr["ludhiana"]);
                chkmangalore.Checked = Convert.ToBoolean(sdr["mangalore"]);
                chkmumbai.Checked = Convert.ToBoolean(sdr["bombay"]);
                chknagpur.Checked = Convert.ToBoolean(sdr["nagpur"]);
                chknashik.Checked = Convert.ToBoolean(sdr["nashik"]);
                chkpatna.Checked = Convert.ToBoolean(sdr["patna"]);
                chkpune.Checked = Convert.ToBoolean(sdr["pune"]);
                chkrajasthan.Checked = Convert.ToBoolean(sdr["rajasthan"]);
                chkthiruvanthapuram.Checked = Convert.ToBoolean(sdr["trivandram"]);
                chktuticorin.Checked = Convert.ToBoolean(sdr["tutucorin"]);
                chkvisakhapatanam.Checked = Convert.ToBoolean(sdr["vishkapattinam"]);
                chkconsolidation.Checked = Convert.ToBoolean(sdr["consolidation"]);
                chkCHA.Checked = Convert.ToBoolean(sdr["CHBA"]);
                cnkFF.Checked = Convert.ToBoolean(sdr["FF"]);
                chkhandling.Checked = Convert.ToBoolean(sdr["handlingliveani"]);
                chkPE.Checked = Convert.ToBoolean(sdr["handlingPA"]);
                chkIATA.Checked = Convert.ToBoolean(sdr["IATAagents"]);
                chkerecplant.Checked = Convert.ToBoolean(sdr["erectionofplant"]);
                chktransp.Checked = Convert.ToBoolean(sdr["liquidtrans"]);
                chkMTO.Checked = Convert.ToBoolean(sdr["MTOagents"]);
                chkODC.Checked = Convert.ToBoolean(sdr["ODCtran"]);
                chkImport.Checked = Convert.ToBoolean(sdr["projimport"]);
                chkhandequip.Checked = Convert.ToBoolean(sdr["supplycranes"]);
                chkbulkcargo.Checked = Convert.ToBoolean(sdr["bulkcargo"]);
                chkcargo.Checked = Convert.ToBoolean(sdr["containercargo"]);
                chkware.Checked = Convert.ToBoolean(sdr["warehousing"]);
            }
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
        }