Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a gridview and when i select a row, i click on a button and the values of that selected row will transfer to another page. But i have a problem "object reference not set to an instance of an object."

Here are the codes

C#
protected void btnEditC_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedRow != null)
        {
            Server.Transfer("~/CustomerEdit.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a row.')", true);
        }


and on the other page

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Page.PreviousPage != null)
        {
            GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
            GridViewRow selectedRow = GridView1.SelectedRow;
            txtStoreID.Text = selectedRow.Cells[1].Text;
            txtStoreName.Text = selectedRow.Cells[2].Text;
            txtStoreOwner.Text = selectedRow.Cells[3].Text;
            txtAddress.Text = selectedRow.Cells[4].Text;
            txtContactNumber.Text = selectedRow.Cells[5].Text;
            txtRoute.Text = selectedRow.Cells[6].Text;
            txtStatus.Text = selectedRow.Cells[7].Text;
       
        }
    }


I tested this code on a another project and it is working fine. Please help.
Posted
Comments
Asim Mahmood 18-Feb-13 2:49am    
Please debug code and check which object is null.
BeastMode10 18-Feb-13 3:00am    
Hello! This is the problem. "GridViewRow selectedRow = GridView1.SelectedRow;"
Asim Mahmood 18-Feb-13 3:16am    
Dear there should be some property assignment missing check your HTML and code properly no issue seems in code.

I heard the same error from many of the developers. The main reason for this error is gridview may not bind (load) its values before fetching the records.. So first bind(load) the gridview controls before accessing its values..

This will applicable for DataList's, drop downlists, checkboxlists, radiobuttonlists etc... If you bind all these controls before fetching its values you may not come accross with the same error..

Try below..

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Page.PreviousPage != null)
        {
            GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
            GridView1.DataBind(); // bind the grid view before selecting the rows..
            GridViewRow selectedRow = GridView1.SelectedRow;
            txtStoreID.Text = selectedRow.Cells[1].Text;
            txtStoreName.Text = selectedRow.Cells[2].Text;
            txtStoreOwner.Text = selectedRow.Cells[3].Text;
            txtAddress.Text = selectedRow.Cells[4].Text;
            txtContactNumber.Text = selectedRow.Cells[5].Text;
            txtRoute.Text = selectedRow.Cells[6].Text;
            txtStatus.Text = selectedRow.Cells[7].Text;
       
        }
    }


...
 
Share this answer
 
Comments
BeastMode10 18-Feb-13 3:31am    
Still the same error.
vinodkumarnie 18-Feb-13 3:38am    
Try to bind the gridview in your privious before redirecting and access the same..
Quote:
This is the problem. "GridViewRow selectedRow = GridView1.SelectedRow;

That simply means there is NO row selected. You should check such a condition.
 
Share this answer
 
Comments
BeastMode10 18-Feb-13 3:10am    
But sir, I tried this on a test project and it is working fine. When I did it on the real project, I have encountered this problem.
Simple: in your page load, you declare a local variable:
GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
This masks the class level version of the control of the same name that you are accessing in the Click event handler - so the external one is never initialized and holds null.
Try taking the word GridView out of that line.
 
Share this answer
 
hi,

try with below code in second page.

C#
protected void Page_Load(object sender, EventArgs e)
{
    Page previousPage = (Page)HttpContext.Current.PreviousHandler;

    if (previousPage  != null)
        {
            GridView GridView1 = previousPage.FindControl("GridView1");
            GridView1.DataBind(); // bind the grid view before selecting the rows..
            GridViewRow selectedRow = GridView1.SelectedRow;
            txtStoreID.Text = selectedRow.Cells[1].Text;
            txtStoreName.Text = selectedRow.Cells[2].Text;
            txtStoreOwner.Text = selectedRow.Cells[3].Text;
            txtAddress.Text = selectedRow.Cells[4].Text;
            txtContactNumber.Text = selectedRow.Cells[5].Text;
            txtRoute.Text = selectedRow.Cells[6].Text;
            txtStatus.Text = selectedRow.Cells[7].Text;       
        }
}


hope it helps.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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