2017-01-30 33 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); 
相關問題