无法将类型为“System.Data.DataRow”的对象强制转换为“System.IConvertible”类型。无法在regid列中存储<system.data.datarow>

时间:2017-01-30 11:27:40

标签: c# asp.net

我很难将数据集值添加到带有for循环的数据表中。请查看代码并帮助我。它会像预期的int32一样抛出错误。

string allCtries = string.Empty;
allCtries = com.COUNTRY_ID;
string[] splitctrs = new string[] { };
splitctrs = allCtries.Split(',');
string m_Query = string.Empty;
DataSet dsPck = new DataSet();
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("regid", typeof(int)),
                                                    new DataColumn("cid", typeof(int)),
                                                    new DataColumn("pid", typeof(int)) });
foreach (string val in splitctrs)
{
    dsPck = con.GetDataSet("select  registrationid, country_id, product_id from registration where product_id = " + com.PRODUCT_ID + " and country_id = " + val + "", CommandType.Text, ConnectionState.Open);
    DataRowCollection drr = dsPck.Tables[0].Rows;
    foreach (var row in drr)
        dt.Rows.Add(row);
    //if (dsPck.Tables[0].Rows.Count> 0)
    //{
    //    dt.Rows.Add(dsPck);
    //}
}

1 个答案:

答案 0 :(得分:0)

使用此

foreach (DataRow dr in dsPck.Tables[0].Rows) 
{
    dt.Rows.Add(dr.ItemArray);
}

代替

DataRowCollection drr = dsPck.Tables[0].Rows;
                foreach (var row in drr)
                    dt.Rows.Add(row);