Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
error:
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: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   sales_order_report.Page_Load(Object sender, EventArgs e) +2610
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

how can i solved this error ?
it is perfct run on my local server but when i publish the website and upload on server this error will come. i use below code for the crystal report in asp.net c#

i use below.

Class1 c = new Class1();
   ReportDocument cr;
   MailMessage msg;
   string id ="", del_id ="",path ="";
   protected void Page_Load(object sender, EventArgs e)
   {
       if (Session["user"] == "" || Session["user"] == null)
       {
           Response.Redirect("default.aspx");
       }
       if (!IsPostBack)
       {
           try
           {
               id = Request.QueryString["id"];
               del_id = Request.QueryString["del_id"];
               c.setcon();
string s = "select id,dealer_order_no,dealer,dealer_id,dealer_name,order_date,product_name,packing,qty,unit,rate,required_date,amount,remarks,file_path from sales_order where dealer_id='" + del_id + "'";
               SqlDataAdapter ad = new SqlDataAdapter(s, c.getcon());
               DataSet ds = new DataSet();
               ad.Fill(ds);

               // file_path = ds.Tables[0].Rows[0][14].ToString();

               path = Server.MapPath("REPORTS/sales_order.rpt");
               cr = new ReportDocument();
               cr.Load(path);
               cr.SetDataSource(ds);
               cr.SetParameterValue(0, del_id);
               CrystalReportViewer1.ReportSource = cr;
               CrystalReportViewer1.DataBind();


               string order_file = string.Empty;
               order_file = Server.MapPath("SALES_ORDER/purchase_order.pdf");
               cr.ExportToDisk(ExportFormatType.PortableDocFormat, order_file);

               string email_id = string.Empty, email_password = string.Empty;
               c.setcon();
               string email_setting = "select email_id,password from send_email";
               SqlDataAdapter email_adapter = new SqlDataAdapter(email_setting, c.getcon());
               DataSet email_dataset = new DataSet();
               email_adapter.Fill(email_dataset);
               c.close();

               if (email_dataset.Tables[0].Rows.Count != 0)
               {
                   email_id = email_dataset.Tables[0].Rows[0][0].ToString();
                   email_password = email_dataset.Tables[0].Rows[0][1].ToString();
               }

               ArrayList region_email = new ArrayList();
               string region_email_address = "select email,region_email,name from empl where id='" + ds.Tables[0].Rows[0][2].ToString() + "'";
               SqlDataAdapter region_ad = new SqlDataAdapter(region_email_address, c.getcon());
               DataSet region_ds = new DataSet();
               region_ad.Fill(region_ds);

               string[] region; // store the region email address of the dealer.
               string dealer_email = string.Empty;
               dealer_email = region_ds.Tables[0].Rows[0][0].ToString(); // store the dealer email address.

               string[] split = region_ds.Tables[0].Rows[0][1].ToString().Split(',');
               region = new string[split.Length];

               for (int i = 0; i < split.Length; i++)
               {
                   region[i] = System.Convert.ToString(split[i]);
               }
               string[] cc_mail; // use to store the CC value for the Mail of a sales order;
               cc_mail = new string[2];
               for (int h = 0; h < cc_mail.Length - 1; h++)
               {
                   cc_mail[h] = dealer_email;
               }

               //cc_mail[1] = "sales@fairmate.net";
               cc_mail[1] = "miteshmachhi1014@yahoo.com";
               SmtpClient client = new SmtpClient();
               client.Host = "smtp.gmail.com";
               client.Port = 587;
               client.Credentials = new NetworkCredential(email_id, email_password);
               client.EnableSsl = true;
               MailAddress from = new MailAddress(email_id);
               msg = new MailMessage();
               msg.From = from;
               foreach (string regionwise_email in region)
               {
                   msg.To.Add(regionwise_email);
               }
               foreach (string cc_email_Send in cc_mail)
               {
                   msg.CC.Add(cc_email_Send);
               }

               string message = "           Please find  attached herewith Purchase Order sent by " + region_ds.Tables[0].Rows[0][2].ToString() + " from FFW.";
               msg.Subject = "Purchase Order - " + region_ds.Tables[0].Rows[0][2].ToString();
               msg.Body = message;

               msg.Attachments.Add(new Attachment(order_file));

               if (!(ds.Tables[0].Rows[0][14].ToString().Equals("")))
               {
                   string files = string.Empty;
                   files = Server.MapPath("PURCHASE_ORDER_FILES/" + ds.Tables[0].Rows[0][14].ToString());
                   cr.ExportToDisk(ExportFormatType.PortableDocFormat, files);
                   msg.Attachments.Add(new Attachment(files));
               }

               client.Send(msg);
               c.close();
               string s1 = "yes";
               Response.Redirect("place_your_order.aspx?order=" + s1 + "");

           }
           catch (Exception e1)
           {
               e1.Message.ToString();
           }
           finally
           {
                  // msg.Dispose();
                  cr.Close();
            }
           }
       }
Posted
Updated 16-Oct-12 22:49pm
v2

The problem is that there are so many possibilities in that method where it could be a null reference - since you do not appear to check at any point that what you are using is valid.
For example:
C#
string email_setting = "select email_id,password from send_email";
SqlDataAdapter email_adapter = new SqlDataAdapter(email_setting, c.getcon());
DataSet email_dataset = new DataSet();
email_adapter.Fill(email_dataset);
c.close();

if (email_dataset.Tables[0].Rows.Count != 0)
You do no check to make sure than any tables have been returned, so if none are, then this will throw a null reference error.

You need to look at your code, and probably run though with the debugger to locate it yourself - there are too many unknowns for us to give you a definitive answer.

BTW: Have a look at the wiki entry on Defensive Programming[^] - it might be an idea if you started using a few ideas from the article... Just sayin'!
 
Share this answer
 
Comments
[no name] 17-Oct-12 5:20am    
i use debugger in local machine but its run perfct without error but publish on server gives this error
It may be possibilities that one of your query is not executing because of not getting the parameters. Run your query by manually executing. If it runs on server than please check your
connection 
of Report.

Thanks
 
Share this answer
 
Comments
[no name] 18-Oct-12 1:53am    
yes, Ashish bhai there is two diffrenet connection on local as well as on Server. I changed it and works perfact.

Thanks a lot for your suggestion......
so my 5++++
Member 10995532 19-Aug-14 2:33am    
Object reference not set to an instance of an object.
error is occuring when displaying crystal reoprt in asp.net
I wrote this below code
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["officeConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT * from course where Cousenm='" +TextBox1.Text+ "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet1 ds = new DataSet1();
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

ReportDocument ReportDoc = new ReportDocument();
ReportDoc.Load(Server.MapPath("CrystalReport.rpt"));
ReportDoc.SetDatabaseLogon(@"USER-PC\SQLEXPRESS", "office");
ReportDoc.SetDataSource(ds);
CrystalReportViewer1.ReportSource = ReportDoc;
ReportDoc.Close();
ReportDoc.Dispose();


please help me

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