Hello all,
First, if I may, a comment. This is my first post. I have been a member of another forum and found that users are treated with little respect... assumptions are made, stupid questions are not allowed patience, etc.
I work for a large IT corporation (if I told you the name you'd all know it) and I'm a completely self taught web programmer. I've made it to my position through the blessings of God and a lot of sweat and hard work. So when I come onto a forum looking for an answer, I've already done my home work.
So, I'm looking for a forum that will treat people kindly, without that competitive pride that can so easily creep it's way into the programmer's world. This site is recommended on the web, while, interestingly enough, the one I belong(ed) to was not even mentioned. So, I'm hoping to find the community here that I'm looking for.
Here's my issue:
I am defining a DataSet
and adding a DataTable
to it and filling the DataTable
with values from a spreadsheet. The cells on the xls(x) can be empty.
I am finding that the DataTable
, when loaded with the spreadsheet values, will replace the empty cells with DBNull
. The issue is that those values with DBNull
will sometimes need to be changed to a String value. When I attempt to change to String
, of course, I get an error.
How can I set up my table so that it will accept String
values where the spreadsheet has had empty cells (now DBNull
).
Here's some code:
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [CRPRList$]", XLSConnect);
DataSet ds = new DataSet();
//somewhere near here (I think) I need to set a configuration so that the xls(x) data is loaded correctly and will not replace cells with DBNull... I could be wrong though
adapter.Fill(ds);
DataTable thisTable = ds.Tables[0];
thisTable.Columns.Add("Summary");
for (int rowNum = 0; rowNum < thisTable.Rows.Count; rowNum++) {
DataRow row = thisTable.Rows[rowNum];
string pr = (row["PR"] == System.DBNull.Value) ? null : row["PR"].ToString();
string cr = (row["CR"] == System.DBNull.Value) ? null : row["CR"].ToString();
try {
if ((pr != "" && pr != null) && (cr == null || cr == "")) {
cr = this.searchForCRNumber(pr.ToString());
row["CR"] = cr; // here is where I get the error
row["PR"] = pr;
}
row["Summary"] = this.searchForSummary(cr);
} catch (Exception ex) {
throw new Exception("PR = " + pr + " and typeof = " + pr.GetType().ToString() + " && CR = " + cr + " and typeof = " + cr.GetType().ToString() + "\nCell typeof = " + row["CR"].GetType().ToString());
//with this try/catch set up I get an error value of "PR = [my xsl value] and typeof = System.String && CR = [string value from method] and typeof = System.String
Cell typeof = System.DBNull"
}
}
thisTable.AcceptChanges();
this.showResults(thisTable);
Thank you, in advance, for the help and I look forward to building a community here!