Click here to Skip to main content
14,304,697 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Am trying to pass values from gridView to a textbox in another page and i get the following error

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

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

protected void Page_Load(object sender, EventArgs e)
    {
        this.txtPatientName.Text = Request.QueryString["name"].ToString();
    }
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,
Try this code below
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.
   
Comments
thatraja 12-Jun-11 13:54pm
   
Right, 5!
Rate this:
Please Sign up or sign in to vote.

Solution 4

Take datakey as name and take gridview deleting command in gridview and change text name of delete command as per you.
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

protected void Page_Load(object sender, EventArgs e)
    {  string Name= Request.QueryString["Name"];
        this.txtPatientName.Text = Name;
    }
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hope this[^] also might help you.
   
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?
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi!

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

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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100