Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
error is.......


Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 29: }
Line 30:
Line 31: dr = dt.NewRow();
Line 32: dr[0] = TextBox1.Text;
Line 33: dr[1] = TextBox2.Text;






my code is 1st page.........


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void btnsave_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("Name", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Address", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Cell", typeof(String)));

        for (int i = 0; i < 1; i++)
        {
            dt = (DataTable)Session["CurrentData"];

        }

            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.Text;
            dt.Rows.Add(dr);
            Session["CurrentData"] = dt;
           
        }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default3.aspx");

    }
    private void clearfields()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    
    
    }
}




2nd page is...........


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        GridView1.DataSource =Session["CurrentData"];
        GridView1.DataBind();
    }
}
Posted
Updated 18-Mar-12 20:11pm
v2

1. Session["CurrentData"];should be checked for null before being used. Most probably this guy is null causing dt to become null and the moment you put a .newrow in front of dt the exception is coming.
 
Share this answer
 
Hey

replace following code
C#
for (int i = 0; i < 1; i++)
        {
            dt = (DataTable)Session["CurrentData"];

        }

by
if(Session["CurrentData"]!=null)
dt = (DataTable)Session["CurrentData"];


and in page load
XML
if(Session["CurrentData"]!=null)
{
   GridView1.DataSource =Session["CurrentData"];
   GridView1.DataBind();
}
else
  GridView1.DataSource=null;
best luck
 
Share this answer
 
hi there,

this error due to because you dt data table object become null thats why it show error Object reference not set to an instance of an object.

so to solve this error please check the value of your session object if it coming then you got your solution.

Regards
Andy.
 
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