Null Exception handle
-
Thursday, April 05, 2012 1:26 PM
the following code how I can handle the null check
Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; Excel.Range range ; string str; string strMake; int rCnt = 0; int cCnt = 0; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(@"E:\ItemList\Item.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3); range = xlWorkSheet.UsedRange; for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++) { if (rCnt >= 2) { strMake = (string)(range.Cells[rCnt, 1] as Excel.Range).Value2.ToString();// Make MessageBox.Show(strMake);if the column 1 hav no value, then following error is coming
'Null reference exception was unhandled' . Please let me know How to handle the null value
With thanks
Pol
polachan
- Moved by Alexander Sun Friday, April 06, 2012 9:20 AM Office Development related (From:Visual C# General)
All Replies
-
Thursday, April 05, 2012 1:38 PM
Just check if the Value2 propriety is null:
var value = (range.Cells[rCnt, 1] as Excel.Range).Value2; if (value != null) strMake = value.ToString();// MakeMarco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva- Proposed As Answer by NorkkMicrosoft Community Contributor Thursday, April 05, 2012 1:39 PM
- Marked As Answer by Yoyo JiangMicrosoft Contingent Staff, Moderator Friday, April 13, 2012 5:14 AM
-
Thursday, April 05, 2012 1:41 PM
Polocahan,
Good Morning, So there's some way for you resolve this question, the first is you realize a check in value and put some value like an avoid string
Is it resolve your problem?
See ya.
Leandro Rodrigues
Caso a resposta tenha ajudado, não esqueça de marcar como resposta válida

