将数据行项从DBNull更改为字符串值

时间:2013-03-19 18:31:36

标签: c# asp.net datatable datarow

我可能做错了,但这里有:

    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值如何,如何将字符串值分配给这些单元格?

1 个答案:

答案 0 :(得分:2)

DBNull不是空字符串,将其转换为字符串不会为您提供null

如果这是您想要的,您应该在将其转换为字符串之前检查它。做类似的事情:

string pr = (row["PR"] == System.DBNull.Value) ? null : row["PR"].ToString();
string cr = (row["CR"] == System.DBNull.Value) ? null : row["CR"].ToString();