none
Unable to upload data from Excel to SQL in C# RRS feed

  • Question

  • Hi Im Trying upload Excel data to SQL server.

    It works fine while running using IIS express.

    When try to run using IIS , Then the following error occurs

    Pls find the error below the code

    string strFileType = Path.GetExtension(FileUpload1.FileName).ToLower();
                        string path = FileUpload1.PostedFile.FileName;
    
                        SqlBulkCopy oSqlBulk = null;
    
                        // SET A CONNECTION WITH THE EXCEL FILE.
                        OleDbConnection myExcelConn = new OleDbConnection
                            ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"");
                            try
                            {
                                myExcelConn.Open();
    
                                // GET DATA FROM EXCEL SHEET.
                                OleDbCommand objOleDB =
                                    new OleDbCommand("SELECT *FROM [Sheet1$] where Avaya_ID IS NOT NULL", myExcelConn);
    
                                // READ THE DATA EXTRACTED FROM THE EXCEL FILE.
                                OleDbDataReader objBulkReader = null;
                                objBulkReader = objOleDB.ExecuteReader();

    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.]
       Avaya_ID.AvayaID.BtnUpload_Click(Object sender, EventArgs e) +1420
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +11764988
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +149
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1664

    Friday, June 1, 2018 7:38 PM

All replies

  • Well, you need to learn how to develop and debug the Web project using VS and local IIS. IIS Express is not the real IIS anyway and is only good for the initial development of the Web solution. But at some point, you must develop  and debug the Web application against local IIS to avoid the issues you are now facing becuase you are now trying to use IIS. 

    Web issues can be addressed at the ASP.NET.

    https://forums.asp.net/

    Object reference not set to an instance of an object.]

    BTW, the exception means that code is trying to access/reference  an object that has not been instanced to be in memory, it's not there and the object is sitting at a null value.

    So it's up to you to debug your code using Visual Stuido and the Web project with IIS using the VS project code in debug mode in order to find the line of code that has a null valued object.
    OO is OO.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    • Proposed as answer by BonnieBMVP Saturday, June 2, 2018 1:31 AM
    Friday, June 1, 2018 10:39 PM
  • Hi Mohammed Salim Mas,

    Thank you for posting here.

    Since your question is more related to IIS, please post a new thread in IIS forum.

    https://forums.iis.net/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 4, 2018 7:03 AM