Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. object reference not set to an instance of an object???

object reference not set to an instance of an object???

Scheduled Pinned Locked Moved C#
helpdatabasecomquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Twyce
    wrote on last edited by
    #1

    I'm using this code(see below)to import data from an excel spreadsheet into my database.now the problem is that it throws "Object reference not set to an instance of an object"exception whenever it gets to this line Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true); what am i missing or what am i doing wrong? plz help public void ImportToStudent() { try { //Gets the filename, not the path string filename = ofd.SafeFileName; string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\..\\PSAtechZTimetableSystem\\SpreadSheet.xls" + "; Extended Properties='Excel 8.0; IMEX=1; HDR=YES'"; // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); using (SqlConnection conPSAtechZ = new SqlConnection(connection)) { conPSAtechZ.ConnectionString = connection; using (SqlCommand commandExcel = conPSAtechZ.CreateCommand()) { System.Data.DataTable table = new System.Data.DataTable(); commandExcel.CommandText = "SELECT [StudentID] AS [StudentID]," + "[ModuleCode] AS [ModuleCode], " + "FROM [Sheet1$]"; //Open the Excel Connection conPSAtechZ.Open(); using (SqlDataReader dr = commandExcel.ExecuteReader(CommandBehavior.CloseConnection)) { while (dr.Read() && dr.HasRows) { lblResults.Text = "Importing..."; try { studentdal.RegisterStudents(new RegisterStudent(Convert.ToString(dr["StudentID"]), Convert.ToString(dr["ModuleCode"]))); commandExcel.CommandType = CommandType.StoredProcedure; lblResults.Text = "Successfully Imported!!"; } catch (Exception Ex) { MessageBox.Show("Error!!\n " + Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } dr.Close(); } } } } catch (Exception EX) { MessageBox.Show("The s

    L M D 3 Replies Last reply
    0
    • T Twyce

      I'm using this code(see below)to import data from an excel spreadsheet into my database.now the problem is that it throws "Object reference not set to an instance of an object"exception whenever it gets to this line Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true); what am i missing or what am i doing wrong? plz help public void ImportToStudent() { try { //Gets the filename, not the path string filename = ofd.SafeFileName; string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\..\\PSAtechZTimetableSystem\\SpreadSheet.xls" + "; Extended Properties='Excel 8.0; IMEX=1; HDR=YES'"; // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); using (SqlConnection conPSAtechZ = new SqlConnection(connection)) { conPSAtechZ.ConnectionString = connection; using (SqlCommand commandExcel = conPSAtechZ.CreateCommand()) { System.Data.DataTable table = new System.Data.DataTable(); commandExcel.CommandText = "SELECT [StudentID] AS [StudentID]," + "[ModuleCode] AS [ModuleCode], " + "FROM [Sheet1$]"; //Open the Excel Connection conPSAtechZ.Open(); using (SqlDataReader dr = commandExcel.ExecuteReader(CommandBehavior.CloseConnection)) { while (dr.Read() && dr.HasRows) { lblResults.Text = "Importing..."; try { studentdal.RegisterStudents(new RegisterStudent(Convert.ToString(dr["StudentID"]), Convert.ToString(dr["ModuleCode"]))); commandExcel.CommandType = CommandType.StoredProcedure; lblResults.Text = "Successfully Imported!!"; } catch (Exception Ex) { MessageBox.Show("Error!!\n " + Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } dr.Close(); } } } } catch (Exception EX) { MessageBox.Show("The s

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Twyce wrote:

      I'm using this code(see below)to import data from an excel spreadsheet into my database.now the problem is that it throws "Object reference not set to an instance of an object"exception whenever it gets to this line

      Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5,
      "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t",
      false, false, 0, true, true, true);

      what am i missing or what am i doing wrong? plz help

      It means that one of the objects you're trying to access is null. Start your application in the debugger, step into this line (it'll automatically when throwing the exception) and see which of them is null.

      1 Reply Last reply
      0
      • T Twyce

        I'm using this code(see below)to import data from an excel spreadsheet into my database.now the problem is that it throws "Object reference not set to an instance of an object"exception whenever it gets to this line Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true); what am i missing or what am i doing wrong? plz help public void ImportToStudent() { try { //Gets the filename, not the path string filename = ofd.SafeFileName; string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\..\\PSAtechZTimetableSystem\\SpreadSheet.xls" + "; Extended Properties='Excel 8.0; IMEX=1; HDR=YES'"; // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); using (SqlConnection conPSAtechZ = new SqlConnection(connection)) { conPSAtechZ.ConnectionString = connection; using (SqlCommand commandExcel = conPSAtechZ.CreateCommand()) { System.Data.DataTable table = new System.Data.DataTable(); commandExcel.CommandText = "SELECT [StudentID] AS [StudentID]," + "[ModuleCode] AS [ModuleCode], " + "FROM [Sheet1$]"; //Open the Excel Connection conPSAtechZ.Open(); using (SqlDataReader dr = commandExcel.ExecuteReader(CommandBehavior.CloseConnection)) { while (dr.Read() && dr.HasRows) { lblResults.Text = "Importing..."; try { studentdal.RegisterStudents(new RegisterStudent(Convert.ToString(dr["StudentID"]), Convert.ToString(dr["ModuleCode"]))); commandExcel.CommandType = CommandType.StoredProcedure; lblResults.Text = "Successfully Imported!!"; } catch (Exception Ex) { MessageBox.Show("Error!!\n " + Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } dr.Close(); } } } } catch (Exception EX) { MessageBox.Show("The s

        D Offline
        D Offline
        Dr Emmett Brown
        wrote on last edited by
        #3

        You posted "twyce" :laugh:

        1 Reply Last reply
        0
        • T Twyce

          I'm using this code(see below)to import data from an excel spreadsheet into my database.now the problem is that it throws "Object reference not set to an instance of an object"exception whenever it gets to this line Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true); what am i missing or what am i doing wrong? plz help public void ImportToStudent() { try { //Gets the filename, not the path string filename = ofd.SafeFileName; string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\..\\PSAtechZTimetableSystem\\SpreadSheet.xls" + "; Extended Properties='Excel 8.0; IMEX=1; HDR=YES'"; // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); using (SqlConnection conPSAtechZ = new SqlConnection(connection)) { conPSAtechZ.ConnectionString = connection; using (SqlCommand commandExcel = conPSAtechZ.CreateCommand()) { System.Data.DataTable table = new System.Data.DataTable(); commandExcel.CommandText = "SELECT [StudentID] AS [StudentID]," + "[ModuleCode] AS [ModuleCode], " + "FROM [Sheet1$]"; //Open the Excel Connection conPSAtechZ.Open(); using (SqlDataReader dr = commandExcel.ExecuteReader(CommandBehavior.CloseConnection)) { while (dr.Read() && dr.HasRows) { lblResults.Text = "Importing..."; try { studentdal.RegisterStudents(new RegisterStudent(Convert.ToString(dr["StudentID"]), Convert.ToString(dr["ModuleCode"]))); commandExcel.CommandType = CommandType.StoredProcedure; lblResults.Text = "Successfully Imported!!"; } catch (Exception Ex) { MessageBox.Show("Error!!\n " + Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } dr.Close(); } } } } catch (Exception EX) { MessageBox.Show("The s

          M Offline
          M Offline
          MarkB777
          wrote on last edited by
          #4

          It's likely the variable ofd is null when you reach the error line.

          Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen Click here to view my blog

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups