none
Error System.NullReferenceException: Object reference not set to an instance of an object RRS feed

  • Question

  • I want to read excel file and save it into dataset then put into database.
    The code is work fine when debugging, means I can upload the file,read it and save into database.
    But after I publish the project.
    It show me error System.NullReferenceException: Object reference not set to an instance of an object.
         
                string path = System.IO.Path.GetFullPath(vault + "/" + FileUpload1.FileName);
                DataSet ds = new DataSet();
                ds = sctt.utility.GetValue.ReadExcelFile(path, "SELECT * FROM [Sheet1$]");     
                string statusid = UCA.Common.Utility.GetValue.getSQLValue("SELECT TOP (1) Asset_Status_ID FROM Asset_Status WHERE (Name = 'Available')");
                if (ds.Tables[0].Rows.Count > 0)

    The error message is at this line  if (ds.Tables[0].Rows.Count > 0)
    Monday, December 16, 2013 9:16 AM

Answers

All replies

  • ds.Tables[0] return null value

    you need to check ds.Tables[0] is null or not

    you also need to check return value of this code

    sctt.utility.GetValue.ReadExcelFile(path, "SELECT * FROM [Sheet1$]")


    Happy Coding, RDRaja


    Monday, December 16, 2013 9:39 AM
  • Hi,

    Better you check first ds is null or not, then check whether the ds contains tables or not then proceed for checking rows.

    try this following code.

     if (ds != null)
     {
        if (ds.Tables.Count > 0)
         {
            if (ds.Tables[0].Rows.Count > 0)
            {

                         // Do what ever you want

           }

          }

    }

    -----------------------------------------

    Mark as answer if it helps to you

    Monday, December 16, 2013 11:14 AM
  • Hi,

    yes. The ds is null when I put if else statement to check it.

    How can I debug it because its works well in debug mode but the dataset is null after i publish the system.

    Tuesday, December 17, 2013 12:37 AM
  • Hi,

    You mean in debug mode dataset is not null after publish the ds is null right better keep a pop message to check that.

    If ds null try to pop a message.

    if (ds != null)
     {
        if (ds.Tables.Count > 0)
         {
            if (ds.Tables[0].Rows.Count > 0)
            {

                         // Do what ever you want

           }

              else

               {

                  MessageBox.Show("No Records Found");

                 }

          }

    else

    {

    MessageBox.Show("No Records Found");

     }

    }

    Tuesday, December 17, 2013 5:19 AM
  • Hi, I had solve the problem using another method : TextFieldParser. Thnx for all reply.
    Tuesday, December 17, 2013 7:34 AM