Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 8th, 2006, 05:31 AM
Authorized User
 
Join Date: Dec 2005
Location: , , .
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);
                }
Reply With Quote
  #2 (permalink)  
Old February 8th, 2006, 09:50 AM
Authorized User
 
Join Date: Dec 2005
Location: , , .
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
Reply With Quote
  #3 (permalink)  
Old November 9th, 2006, 07:44 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #4 (permalink)  
Old March 5th, 2009, 04: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)
Reply With Quote
  #5 (permalink)  
Old March 5th, 2009, 04: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)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel VBA Check When Cell Has Changed tonyrosen Excel VBA 12 March 3rd, 2013 09:04 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 06:19 AM
check a cell to see if it has a currency code crmpicco Excel VBA 3 December 19th, 2005 09:10 AM



All times are GMT -4. The time now is 10:04 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.