2017-01-30 93 views
-1

我被卡在使用for循环添加数据集到数据表中。请参阅代码并帮助我。它抛出像预期的int32错误。无法投射'System.Data.DataRow'类型的对象来键入'System.IConvertible'.Couldn不存储<System.Data.DataRow>在regid列

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); 
    //} 
} 
+0

你是否已经tryed使用的Int32不是int? – Sergio

+0

是的,我试过int32,int64也是抛出错误。 – Ayyappa

+0

在这个地方抛出错误dt.Rows.Add(row); – Ayyappa

回答

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); 
相关问题