I'm parsing an excel document into a System.Array and then using this data to develop a DataTable. The problem is that not every element in the System.Array contains a string and is null. See image below:
My question is how to I test for the null case.
Here is my method:
public static System.Data.DataTable ConvertCSVtoDataTable(string strFilePath)
{
System.Data.DataTable dt = new System.Data.DataTable();
StreamReader sr = new StreamReader(strFilePath);
string[] headers = sr.ReadLine().Split(',');
foreach (string header in headers)
dt.Columns.Add(header);
sr.Close();
Microsoft.Office.Interop.Excel.Workbook wb = null;
Microsoft.Office.Interop.Excel.Worksheet ws = null;
Microsoft.Office.Interop.Excel.Application app = null;
int lastRow = 0;
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
wb = app.Workbooks.Open(strFilePath);
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
lastRow = ws.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell).Row;
int currentRow = 0;
for (int index = 2; index <= lastRow; index++)
{
System.Array values = (System.Array)ws.get_Range("A" + index.ToString(), "M" + index.ToString()).Cells.Value;
dt.Rows.Add();
dt.Rows[currentRow]["Plugin ID"] = values.GetValue(1, 1).ToString();
dt.Rows[currentRow]["CVE"] = values.GetValue(1, 2).ToString();
dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
dt.Rows[currentRow]["Risk"] = values.GetValue(1, 4).ToString();
dt.Rows[currentRow]["Host"] = values.GetValue(1, 5).ToString();
dt.Rows[currentRow]["Protocol"] = values.GetValue(1, 6).ToString();
dt.Rows[currentRow]["Port"] = values.GetValue(1, 7).ToString();
dt.Rows[currentRow]["Name"] = values.GetValue(1, 8).ToString();
dt.Rows[currentRow]["Synopsis"] = values.GetValue(1, 9).ToString();
dt.Rows[currentRow]["Description"] = values.GetValue(1, 10).ToString();
dt.Rows[currentRow]["Solution"] = values.GetValue(1, 11).ToString();
dt.Rows[currentRow]["See Also"] = values.GetValue(1, 12).ToString();
dt.Rows[currentRow]["Plugin Output"] = values.GetValue(1, 13).ToString();
currentRow++;
}
return dt;
}
Here are some checks I've tried but didn't work
if (!values.Equals(null))
dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
else
dt.Rows[currentRow]["CVSS"] = "";
It appears this doesn't work because it seems like it is checking to see if any elements exist, not if the specfic element at this location is null.
if (values.GetValue(1, 3).ToString() == null)
dt.Rows[currentRow]["CVSS"] = "";
else
dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
This throws a NullReferenceExecption: Object reference not set to an instance of an object.
values.Equals(null)
will throw on null
, it won't return true
.anything.ToString()
will also throw on null
, it won't return a null
(or empty) string reference.What you want to do is probably just:
if(values.GetValue(1, 3) == null) ...
Change if (values.GetValue(1, 3).ToString() == null)
to
if (values.GetValue(1, 3) == null)
if
, as you have written it could be "translated" to if(null.ToString() ...)
in the case the value itself is null.
And you can't call anything on null
.
You could use the null conditional operator (?.) in conjunction with Null-Coalescing Operator (??) like this:
dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3)?.ToString() ?? ""
With the null conditional operator (?.) GetValue(1,3) will be evaluated only if it not null. If it null dt.Rows[currentRow]["CVSS"] will have a string empty value ("").
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.