【发布时间】:2013-03-19 18:31:35
【问题描述】:
我可能做错了,但这里是:
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [CRPRList$]", XLSConnect);
//the XLSConnect is from a spreadsheet. I've confirmed the data is there.
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable thisTable = ds.Tables[0];
//The table exists and it does populate
thisTable.Columns.Add("Summary");
//The new column is added and I am able to populate it (below)
for (int rowNum = 0; rowNum < thisTable.Rows.Count; rowNum++) {
//Here I'm cycling through the row items
DataRow row = thisTable.Rows[rowNum];
string pr = row["PR"].ToString();
string cr = row["CR"].ToString();
//The first two column values are grabbed and examined
if ((pr != "" && pr != null)&&(cr == null || cr == "")) {
//if the first column is empty then the second is examined and I am attempting
//to populate the first column using the searchForCRNumber method
//This method returns a string
cr = this.searchForCRNumber(pr); //assignment works
row[0] = cr; //Here is the problem, this assignment does not work (more explained below)
}
row["Summary"] = this.searchForSummary(cr);
row.AcceptChanges();
}
this.showResults(thisTable);//works, except the changes above are not applied
row[0] = cr; 行自然是一个空值,因此它作为 DBNull 类型出现,并且不会接受 cr 字符串。
我得到的错误是System.FormatException: Input string was not in a correct format.
我已经搜索了将row[0] 转换为空字符串的方法或转换cr 的其他方法,但到目前为止我还没有找到方法。
编辑 我正在添加更多内容以帮助了解围绕此问题发生的情况。我认为我在上面已经足够彻底了,但也许不是......
XLSConnect 的数据来自:
private bool XSLConnection(string filePath, string fileType) { // returns false if XSL or XSLX is not the fileType
string strConn;
if (fileType.ToLower() == "xls" || fileType.ToLower() == "xlsx") {
strConn = (fileType == "xlsx") ? string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", filePath) : string.Format("Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0\"", filePath);
XLSConnect = new OleDbConnection(strConn);
return true;
} else return false;
}
这是searchForCRNumber 方法:
private string searchForCRNumber(string PRNumber) {
return this.getProperty("MyReturnColumn", PRNumber, "MyMatchColumn");
}
这是getProperty 方法:
private string getProperty(string searchProperty, string searchVal, string rtrnProperty) {
string undefined = "Undefined";
string rtrn = null;
string query = "SELECT " + rtrnProperty + " FROM MySQLTable WHERE " + searchProperty + " = '" + searchVal + "'";
this.QCConn.Open();
SqlDataReader reader = this.runQCConnect(query).ExecuteReader();
reader.Read();
rtrn = (!reader.HasRows)?
undefined :
reader[0].ToString();
if (rtrn == "" || rtrn == null) rtrn = undefined;
this.QCConn.Close();
return rtrn;
}
更多编辑 我仍在研究这个,我开始怀疑我是否还没有发现错误。我发现了问题所在,并且仍在努力解决问题。上传的 xls(x) 可以有空单元格。我发现那些空单元格会导致 DataTable 有空单元格,但它们的类型是 DBNull ......当然,不能(自然)分配字符串值。
当我在 try/catch 中应用以下内容时:
throw new Exception("PR = " + pr + " and typeof = " + pr.GetType().ToString() + " && CR = " + cr + " and typeof = " + cr.GetType().ToString() + "\nCell typeof = " + row["CR"].GetType().ToString());
我收到以下回复:
PR = 7800 and typeof = System.String && CR = Undefined and typeof = System.String
Cell typeof = System.DBNull
如您所见,该单元格是 System.DBNull 并且不接受字符串值。这是我错误的根源。但我仍然不知道如何使这项工作。有什么我想念的吗?无论 DBNull 值如何,如何将字符串值分配给这些单元格?
【问题讨论】:
-
你能发布 searchForCRNumber(pr) 过程吗?
-
row[0] = cr;这不是问题所在。这不会导致该异常。 -
我可以,但是,它确实按预期返回了一个字符串。这一点应该没有实际意义。
-
row[0] 以
DBNull类型出现,而 cr 以string类型出现。这是调试显示问题的地方 -
@Jimmmy:但是,您并没有返回索引属性,而是在设置它。这可能会导致several exceptions,但不会导致
FormatException。
标签: c# asp.net datatable datarow