Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:53:08+00:00 2026-06-08T11:53:08+00:00

Hi I keep getting this error from the below code, was wondering if anyone

  • 0

Hi I keep getting this error from the below code, was wondering if anyone can help.

error processing excel file: cannot perform runtime binding on a null reference

Code:

    private void Import_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

        // Show open file dialog box
        Nullable<bool> result = dlg.ShowDialog();

        // Process open file dialog box results
        if (result == true)
        {
            // Open document
            string filename = dlg.FileName;

            Microsoft.Office.Interop.Excel.Application vExcelObj = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                Microsoft.Office.Interop.Excel.Workbook theWorkbook = vExcelObj.Workbooks.Open(filename, Type.Missing, true);

                Microsoft.Office.Interop.Excel.Worksheet sheet = theWorkbook.Worksheets[1];  

                string vFirstName = "temp";
                string vLastName = "temp";
                int vIndex = 1;

                while (vFirstName != "")
                {
                    // Change the letters of the appropriate columns here!  
                    // In my example, 'A' is first name, 'B' last name
                    vFirstName = sheet.get_Range("A" + vIndex.ToString()).Value.ToString(); // if i take out the exception handling the error is on this line
                    vLastName = sheet.get_Range("B" + vIndex.ToString()).Value.ToString();


                    this.SaveNewCustomer(vFirstName, vLastName); 

                    vIndex++;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error processing excel file : " + ex.Message);
            }
            finally
            {
                vExcelObj.Quit();
            }
        }
    }

    private void SaveNewCustomer(string firstName, string lastName) 
    {
        string uri = "http://localhost:8002/Service/Customer";
        StringBuilder sb = new StringBuilder();
        sb.Append("<Customers>");
        sb.AppendLine("<FirstName>" + firstName + "</FirstName>");
        sb.AppendLine("<LastName>" + lastName + "</LastName>");
        sb.AppendLine("</Customers>");
        string NewStudent = sb.ToString();
        byte[] arr = Encoding.UTF8.GetBytes(NewStudent);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
        req.Method = "POST";
        req.ContentType = "application/xml";
        req.ContentLength = arr.Length;
        Stream reqStrm = req.GetRequestStream();
        reqStrm.Write(arr, 0, arr.Length);
        reqStrm.Close();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        reqStrm.Close();
        resp.Close();
    }

}

The code just takes a excel document and trys to send the data to my web service.

So I tryed using the below method but it freezes the application :S no error just hangs.
Edit attempt:

                while (vFirstName != "")
                {
                        var columnACell = sheet.get_Range("A" + vIndex.ToString());
                        var columnBCell = sheet.get_Range("B" + vIndex.ToString());
                        var columnACellValue = columnACell.Value;
                        var columnBCellValue = columnBCell.Value;


                    if (columnACellValue != null && columnBCellValue != null)
                    {
                        vFirstName = columnACellValue.ToString();
                        vLastName = columnBCellValue.ToString();

                        this.SaveNewStaff(vFirstName, vLastName); //, vPassword

                        vIndex++;
                    }
                }
            }

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:53:09+00:00Added an answer on June 8, 2026 at 11:53 am

    EDIT 2

    Just took the code, and stepped through it. Found the problem. I think I misunderstood what was happening originally.

    What’s happening is that the loop while (vFirstName != "") will keep going until vFirstName is an empty string. But this will never happen! Here’s why:

    • Everything will be fine as long as columns A and B will have values. The code will behave as expected.
    • When the code gets to an Excel row that doesn’t have a value, it hits an empty cell, which will have .Value set to null. This causes the exception.

    So the real solution here is to have the loop keep going until it hits a cell with a null value, and then exit. Kind of like this:

    while (true) {
        // Split the satements 
        var columnACell = sheet.get_Range("A" + vIndex.ToString());
        var columnBCell = sheet.get_Range("B" + vIndex.ToString());
        var columnACellValue = columnACell.Value;
        var columnBCellValue = columnBCell.Value;
    
        if (columnACellValue != null && columnBCellValue != null) {
            vFirstName = columnACellValue.ToString();
            vLastName = columnBCellValue.ToString();
    
        } else {
            break;
        }
    
        this.SaveNewCustomer(vFirstName, vLastName);
    
        vIndex++;
    
    };
    

    Just tested this on my end, and it seems to work.

    On a separate note, make sure that you’re fully quitting Excel, because calling Excel.Quit() is often not enough. Open Task Manager and check whether there are any extra instances of EXCEL.exe floating around. To prevent those I usually kill Excel after I’m done with it (easier than properly releasing Excel’s COM objects), as described in this post.


    ORIGINAL POST

    It sounds like there are a few options here:

    • The cell is empty, which means that it’s .Value will be null.
    • The sheet is null,
    • get_Range() returns null — that sounds unlikely.

    Split the line into separate statements and see which one of them throws an error. That will tell you where to look further.

    Judging by what you’re doing — searching the column until you find first name — it sounds like you’re running into nulls inside the cells’ Values. To deal with that, I usually add a quick if-statement to test Value for null.

    EDIT

    Here’s an example (may not compile) that will hopefully fix null values inside the cells and help to pinpoint other null-related problems. Replace the offending lines with something like this:

    var columnACell = sheet.get_Range("A" + vIndex.ToString());
    var columnBCell = sheet.get_Range("B" + vIndex.ToString())
    var columnACellValue = columnACell.Value;
    var columnBCellValue = columnBCell.Value;
    
    if (columnACellValue != null && columnBCellValue != null) {
        vFirstName = columnACellValue.ToString(); 
        vLastName = columnBCellValue.ToString();
    }
    

    Note that I assume that your C# compiler supports implicit static typing through var.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this error message from the code below. What am I doing
Does anyone know why I keep getting this error below? I can't seem to
I keep getting this error on emulator: I copied this from a tutorial and
Hi I keep getting this error, I'm trying to get tweets from twitter via
I keep getting this error System.Web.HttpException was unhandled by user code Message=Validation of viewstate
I keep getting this error on my views. I can't work it out as
As the title says, keep getting this error when trying to compile. From Googling
I keep getting this error for the below MySQL SP any ideas? CREATE PROCEDURE
I keep keep getting the above error when I run the code below. All
I keep getting this error when I try to load my templates: TypeError: cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.