p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
I forgot my password Register Now
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 8th, 2006, 06: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);
                }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old February 8th, 2006, 10: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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old November 9th, 2006, 08: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() ?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old March 5th, 2009, 05:53 AM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old March 5th, 2009, 05:54 AM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I check if a cell content has changed SirAldemar Access VBA 2 August 13th, 2008 08:46 AM
Find the rowcount before blank cell stepdev Excel VBA 6 May 22nd, 2006 04: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
Excel VBA Check When Cell Has Changed tonyrosen Excel VBA 9 November 16th, 2005 11:52 AM



All times are GMT -4. The time now is 05:19 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc