2017-01-30 12 views
-1

forループを使用してデータセットにデータセット値を追加することができません。コードを見て助けてください。予想されるint32のようなエラーをスローします。タイプ 'System.Data.DataRow'のオブジェクトを 'System.IConvertible'にキャストすることができません。regidに<System.Data.DataRow>を格納できません。列

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

すでに代わりにint型使用のInt32をしようと試みたことがありますか? – Sergio

+0

はい.iも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); 
関連する問題