Click here to Skip to main content
16,004,505 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi Am trying to pass values from gridView to a textbox in another page and i get the following error

MSIL
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 10:     protected void Page_Load(object sender, EventArgs e)
Line 11:     {
Line 12:         this.txtPatientName.Text = Request.QueryString["name"].ToString();
Line 13:     }
Line 14: }



Below is the code from my first page

C#
foreach (GridViewRow row in grdAvailableStuff.Rows)
       {
           string name = row.Cells[1].Text;
           string time = row.Cells[2].Text;
           Response.Redirect("AddPatients.aspx?name=" + name.ToString());
       }


Then below is my second page

C#
protected void Page_Load(object sender, EventArgs e)
    {
        this.txtPatientName.Text = Request.QueryString["name"].ToString();
    }
Posted

Hi,
Try this code below
C#
if (Request.QueryString["name"]!=null && !string.IsNullOrEmpty(Request.QueryString["name"].ToString()))
{
    this.txtPatientName.Text = Request.QueryString["name"].ToString();
}


And on first page why each gridview row is redirecting name value to other page? In this case it wont work, redirection happen based on some event/business logics. Give this a thought.Hope this will solve your Object reference error.
 
Share this answer
 
Comments
thatraja 12-Jun-11 13:54pm    
Right, 5!
Take datakey as name and take gridview deleting command in gridview and change text name of delete command as per you.
C#
protected void EmployeeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string Name= EmployeeGridView.DataKeys[e.RowIndex].Value.ToString();
    Response.Redirect("AddPatients.aspx?EmployeeId=" + Name);
}





then on load event of next page
do something

C#
protected void Page_Load(object sender, EventArgs e)
    {  string Name= Request.QueryString["Name"];
        this.txtPatientName.Text = Name;
    }
 
Share this answer
 
Hope this[^] also might help you.
 
Share this answer
 
Comments
Anele Ngqandu 13-Jun-11 6:57am    
K i like the solution, my problem is am using hyperlinkField and navigationUrl="addpatient.aspx?name={0}".
Now the problem is navigationUrl passes this value "{0}", how can i pass the selected value inside a column of a gridview?
Hi!

You can Redirect to other page also if name Request is empty like this

C#
if (Request.QueryString["name"]!=null && _
    !string.IsNullOrEmpty(Request.QueryString["name"].ToString()))
{
    this.txtPatientName.Text = Request.QueryString["name"].ToString();
}

else
{
    My.Response.Redirect = "~\Default.aspx"
}


Thanks.
 
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