Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 8th, 2006, 06:31 AM
Authorized User
 
Join Date: Dec 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Check if a cell is blank in excel

Hi everyone,

I am having difficulty the the System.NullException error during runtime. As I parse through an excel spreadsheet, the program needs to detect whether a cell contains any value or not. Now I know the particular cell it is trying to read is blank and need to know how to avoid getting the nullexception error (should be some blank checking function). I am not sure if it's the way I search the net but had no luck finding an appropriate solution. Please someone help!!

Here is the code I wrote so far
Code:
                try {
                    if (((Excel.Range)xlSheet.Cells[i, "B"]).Value2.ToString() != null) {
                        System.Diagnostics.Trace.WriteLine(xlSheet.Cells[i, "B"].ToString());
                        nominalCode = ((Excel.Range)xlSheet.Cells[i, "B"]).Value2.ToString();
                        System.Diagnostics.Trace.WriteLine(nominalCode);
                    }
                    if (((Excel.Range)xlSheet.Cells[i, "D"]).Value2.ToString() != null) {
                        nominalCodeName = ((Excel.Range)xlSheet.Cells[i, "D"]).Value2.ToString();
                        System.Diagnostics.Trace.WriteLine(nominalCodeName);
                    }
                    if (((Excel.Range)xlSheet.Cells[i, "E"]).Value2.ToString() != null) {
                        dept = ((Excel.Range)xlSheet.Cells[i, "E"]).Value2.ToString();
                        System.Diagnostics.Trace.WriteLine(dept);
                    }
                    if (((Excel.Range)xlSheet.Cells[i, "K"]).Value2.ToString() != null) {
                        balance = Convert.ToInt32(((Excel.Range)xlSheet.Cells[i, "K"]).Value2.ToString());
                        System.Diagnostics.Trace.WriteLine(balance);
                    }
                    else {
                        balance = 0;
                    }
                    if (((Excel.Range)xlSheet.Cells[i, "L"]).Value2.ToString() != null) {

                        debit = Convert.ToInt32(((Excel.Range)xlSheet.Cells[i, "L"]).Value2.ToString());
                        System.Diagnostics.Trace.WriteLine(debit);
                    }
                    else {
                        debit = 0;
                    }
                    if (((Excel.Range)xlSheet.Cells[i, "Q"]).Value2.ToString() != null) {
                        credit = Convert.ToInt32(((Excel.Range)xlSheet.Cells[i, "Q"]).Value2.ToString());
                        System.Diagnostics.Trace.WriteLine(credit);
                    }
                    else {
                        credit = 0;
                    }
                    if (((Excel.Range)xlSheet.Cells[i, "S"]).Value2.ToString() != null) {
                        totalDebit = Convert.ToInt32(((Excel.Range)xlSheet.Cells[i, "S"]).Value2.ToString());
                        System.Diagnostics.Trace.WriteLine(totalDebit);
                    }
                    else { 
                        totalDebit = 0;
                    }
                    if (((Excel.Range)xlSheet.Cells[i, "X"]).Value2.ToString() != null) {
                        totalCredit = Convert.ToInt32(((Excel.Range)xlSheet.Cells[i, "X"]).Value2.ToString());
                        System.Diagnostics.Trace.WriteLine(totalCredit);
                    }
                    else {
                        totalCredit = 0;
                    }

                    DataRow row = fileTable.NewRow();

                    System.Diagnostics.Trace.WriteLine(nominalCode + " " + nominalCodeName + " " + dept + " " + balance + " " + totalCredit);
                    // Enter each data into struct

                    row["NC"] =                    nominalCode;
                    row["NCName"] =                nominalCodeName;
                    row["Dept"] =                dept;
                    row["Balance"] =            balance;
                    row["DebitCredit"] =        debit - credit;
                    row["totalDebitCredit"] =    totalDebit - totalCredit;
                    fileTable.Rows.Add(row);
                }
                catch (Exception err) {
                    System.Console.WriteLine("{0} Exception caught.", err);
                }
 
Old February 8th, 2006, 10:50 AM
Authorized User
 
Join Date: Dec 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Found the solution, or atleast something that works.

Change the red bit to the following

Code:
Excel.Range)xlSheet.Cells[i, "E"]).Value2 != null
 
Old November 9th, 2006, 08:44 AM
Registered User
 
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So, all you did was remove the .ToString() ?

 
Old March 5th, 2009, 05:53 AM
Registered User
 
Join Date: Mar 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Check blank excel sheet Cell in c#

Check the condition like this

((Excel.Range)xlSheet.Cells[i, "B"]).Value2 != null)

not like

((Excel.Range)xlSheet.Cells[i, "B"]).Value2.ToString() != null)
 
Old March 5th, 2009, 05:54 AM
Registered User
 
Join Date: Mar 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Check blank excel sheet Cell in c#

Check the condition like this

((Excel.Range)xlSheet.Cells[i, "B"]).Value2 != null)

not like

((Excel.Range)xlSheet.Cells[i, "B"]).Value2.ToString() != null)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel VBA Check When Cell Has Changed tonyrosen Excel VBA 13 March 24th, 2015 05:15 PM
How do I check if a cell content has changed SirAldemar Access VBA 2 August 13th, 2008 07:46 AM
Find the rowcount before blank cell stepdev Excel VBA 6 May 22nd, 2006 03:05 PM
check if a cell has a bottom border crmpicco Excel VBA 2 January 10th, 2006 07:19 AM
check a cell to see if it has a currency code crmpicco Excel VBA 3 December 19th, 2005 10:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.