Unable to cast object of type 'System.Data.EnumerableRowCollection`1[System.Data.DataRow]' to same type
I am getting this error when I publish my code to 开发者_JS百科the server (GoDaddy), but locally when I run in code
Unable to cast object of type
'System.Data.EnumerableRowCollection`1[System.Data.DataRow]' to type
'System.Data.EnumerableRowCollection`1[System.Data.DataRow]'
The code that the error is triggering on is:
foreach (DataRow dr in ds.Tables[0].Rows)
{
error occuring here ==> var images = (from DataRow myRow in ds.Tables[1].AsEnumerable()
where (int)myRow["ProductID"] == Convert.ToInt32(dr["ProductID"])
select myRow);
Product product = new Product(dr, images);
productCollection.Add(product);
}
I don't understand how it cannot cast one type to the same type and why it would only happen on the web server. The domain is set up on the 3.5 framework, other linq queries execute successfully, so I am at a loss.
Any suggestions?
Are you about the line that causes the error?
I would expect it here:
Product product = new Product(dr, images);
But the fact that it does not happen on your dev system but only after deployment suggests that an assembly is out of sync. Rebuild at the server and/or make sure you push out all assemblies.
I had something very similar and spend hours wrapping my head around it. In my case I had
EnumerableRowCollection<DataRow> ercBarriers = tblReviewInfo.AsEnumerable().Cast<DataRow>().Where(x => !string.IsNullOrEmpty(x["Outcomes"].ToString()));
After removing .Cast() everything worked, which leads me to wonder if you don't have corrupted data. Allow me to explain: you have two places where you assume an integer. If either one fails an error is thrown, although I am not certain if this exact error. Can you test your code on data that you know for a fact contains all integers? Also make sure they are not huge numbers that require a cast as a double. Just a hunch.
精彩评论