Click here to Skip to main content
15,034,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have the output but when i tried to click at the open button and select the excel file that i need, the file cannot be opened...

and this is what i get :
An unhandled exception of type 'System.NullReferenceException' occurred in myproject.exe

Additional information: Object reference not set to an instance of an object.

the procedure entry point ucrtbase.terminate could not be located in the dynamic link library api-ms-win-crt-runtime-|1-1-0.dll.

+ $exception {System.NullReferenceException: Object reference not set to an instance of an object.
at myproject.Form1.btnopen_Click(Object sender, EventArgs e) in c:\Users\toshiba\Documents\Visual Studio 2012\Projects\myproject\myproject\Form1.cs:line 33
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at myproject.Program.Main() in c:\Users\toshiba\Documents\Visual Studio 2012\Projects\myproject\myproject\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()} System.Exception {System.NullReferenceException}
+ ofd {System.Windows.Forms.OpenFileDialog: Title: , FileName: C:\Users\toshiba\Documents\PRODUCT.xlsx} System.Windows.Forms.OpenFileDialog

What I have tried:

using Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace myproject
    public partial class Form1 : Form
        public Form1()

        DataSet result;

        private void btnopen_Click(object sender, EventArgs e)
            using (OpenFileDialog opfd = new OpenFileDialog() { Filter = "Excel Workbook|*.xlsx", ValidateNames = true })
                if (opfd.ShowDialog() == DialogResult.OK)
                    FileStream fs = File.Open(opfd.FileName, FileMode.Open, FileAccess.Read);
                    IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
                    reader.IsFirstRowAsColumnNames = true;
                    result = reader.AsDataSet();
                    foreach (DataTable dt in result.Tables)

        private void Sheetname_SelectedIndexChanged(object sender, EventArgs e)
            dataview.DataSource = result.Tables[Sheetname.SelectedIndex];
Updated 11-Jul-17 22:07pm

Start by using the debugger to find out exactly where the problem is:
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

The problem is probably something to do with your file system or your app having the file open already, but without the error message and the exact line it occurs on we can't even begin to guess what will cure it! So use the debugger, check all the variable contents, and see what you can find.
If you can't spot the problem from that, then come back to us with actual information and we'll see what we can do - but we don;t have access to your file system, and you need your app running to find the problem.
Member 13305578 12-Jul-17 4:43am
tq for the answer :)
You are using the wrong reader type: Using IExcelDataReader[^].
Member 13305578 12-Jul-17 4:15am
tq! i've fix the problem :)

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