Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to pass values from gridView of one page to a textbox of another page
I have written following code
On first page:
C#
foreach (GridViewRow row in dgvSearchResults.Rows)
       {
           string name = row.Cells[1].Text;
           string time = row.Cells[2].Text;
           Response.Redirect("EmpDetails.aspx?name=" + name.ToString());
       }


on second page:
this.txtPatientName.Text = Request.QueryString["name"].ToString();


When I am trying this to do,I am getting following error:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 
Source Error:
Line 108:     protected void Page_Load(object sender, EventArgs e)
Line 109:     {
Line 110:         this.txtName.Text = Request.QueryString["name"].ToString();
Line 111:     }
Line 112: }


Where is the problem?
What should i need to do to solve this?
Posted
Comments
Sandeep Mewara 30-Apr-12 1:10am    
<pre lang="cs">foreach (GridViewRow row in dgvSearchResults.Rows)
{
string name = row.Cells[1].Text;
string time = row.Cells[2].Text;
Response.Redirect("EmpDetails.aspx?name=" + name.ToString());
}</pre>
Any idea on how the above code works or what the execution is here? I would suggest you to put a DEBUG point and see. Not a correct way of writing code. DEBUG the execution and share what you observe.

Try this code on second page:
C#
if (Request.QueryString["name"]!=null && !string.IsNullOrEmpty(Request.QueryString["name"].ToString()))
{
    this.txtName.Text = Request.QueryString["name"].ToString();
}


Or else have a look here[^]
 
Share this answer
 
v3
Comments
Sandeep Mewara 30-Apr-12 1:02am    
This answer is just to handle the error. We should always check possible null-reference before using it. Agreed. But, this will not solve OP's problem here.
You can use Cross-Page Posting to access DataGrid value in another page. Have a look:
msdn.microsoft.com/en-us/library/ms178139.aspx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900