c# - Query String Value always 0 -


this easy one, cannot see wrong..

i have string being added url: /projects.aspx?pid=3

and in code behind trying grab value so:

public partial class projects : system.web.ui.page {     public int pid { get; set; }      protected void page_load(object sender, eventargs e)     {         if (!page.ispostback)         {             pid = convert.toint32(request.querystring["pid"]); //this returning 0 when should have value.             databind(true);              populatetestsuitelist();         }          testsuitesdatalist.itemcommand += new repeatercommandeventhandler(this.item_command);     } 

when try @ value of pid in other functions, gives me '0'. don't see why won't give me 3 should?

here example functions:

private void executeinsert(string testsuitename, string testsuitedescription, int projectid)     {         sqlconnection conn = new sqlconnection(getconnectionstring());         string sql = "insert testsuites (projectid, testsuitename, testsuitedescription) values " + " (@projectid,@testsuitename,@testsuitedescription)";         try         {             conn.open();             sqlcommand cmd = new sqlcommand(sql, conn);              sqlparameter[] param = new sqlparameter[3];              param[0] = new sqlparameter("@testsuitename", sqldbtype.nvarchar, 50);             param[1] = new sqlparameter("@testsuitedescription", sqldbtype.nvarchar, 200);             param[2] = new sqlparameter("@projectid", sqldbtype.int);              param[0].value = testsuitename;             param[1].value = testsuitedescription;             param[2].value = pid;              (int = 0; < param.length; i++)             {                 cmd.parameters.add(param[i]);             }              cmd.commandtype = commandtype.text;             cmd.executenonquery();         }         catch (system.data.sqlclient.sqlexception ex)         {             string msg = "insert error:";             msg += ex.message;             throw new exception(msg);         }                 {             conn.close();         }     }      protected void addtestsuitebutton_click(object sender, eventargs e)     {         //checks whether test suite name exists in database , stops duplicates being added same project         string checkexistsquery = "select testsuitename testsuites testsuitename = @testsuitename , @projectid = " + pid;          sqlconnection conn = new sqlconnection(getconnectionstring());         sqlcommand checkexistscmd = new sqlcommand(checkexistsquery, conn);          sqlparameter c1 = new sqlparameter("testsuitename", txttestsuitename.text);         sqlparameter c2 = new sqlparameter("projectid", pid);          checkexistscmd.parameters.add(c1);         checkexistscmd.parameters.add(c2);          conn.open();          sqldatareader rd = checkexistscmd.executereader();          if (rd.hasrows)         {             lbladdtestsuitemessage.text = "oops, project name exists.";             rd.close();             conn.close();         }         else         {             string formattedtestsuitedescription = txttestsuitedescription.text.replace("\r\n", "<br />");             //call method execute insert database             executeinsert(txttestsuitename.text, formattedtestsuitedescription, pid);              lbladdtestsuitemessage.text = "project has been added successfully";             clearcontrols(page);         }          rd.close();         conn.close();         response.redirect(request.rawurl);     } 

in same code behind, function seems happily pulling correct data , displaying on page - using pid well??

        private void populatetestsuitelist()     {         sqlconnection conn = new sqlconnection(getconnectionstring());         conn.open();         string sql = "select testsuiteid, projectid, testsuitename, testsuitedescription testsuites projectid = " + pid + "order testsuitename asc";         sqlcommand cmd = new sqlcommand(sql, conn);          sqldatareader rd = cmd.executereader();         while (rd.read())         {             testsuitesdatalist.datasource = rd;             testsuitesdatalist.databind();         }         conn.close();     } 

this caused not understanding page life cycle. setting pid on page_load, using somewhere when page not yet loaded or re-loaded (e.g. after postback).

if pid value query string , need use on postback, need store in session variable or hidden field on page. assigning page class variable not persist doing here.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -