Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
I received the error:"Unable to cast object of type 'System.Data.DataRow' to type 'System.Data.DataRowView'. "
 
private void addToOrder(DataRowView drv)
        {
            DataRow dr = orderDataTable.NewRow();
            dr["ProductID"] = drv["ProductID"];
            dr["ProductBarcode"] = drv["ProductBarcode"];
            dr["ProductName"] = drv["ProductName"];
            dr["SalePrice"] = drv["SalePrice"];
            dr["SoldQuantity"] = 1;
            dr["NotEditable"] = false;
            try
            {
                orderDataTable.Rows.Add(dr);
            }
            catch (ConstraintException)
            {
                DataRow dr1 = orderDataTable.Rows.Find(drv["ProductID"]);
                dr1["SoldQuantity"] = Convert.ToInt32(dr1["SoldQuantity"]) + 1;
            }
        }
private void btnAddToOrder_Click(object sender, RoutedEventArgs e)
        {
            // add to order
            if(txtBarcodeAuto.Text.Length >0)
            {
                DataTable dscrntProd = prods.SelCrntProd4Sale(crntProdID);//get data from sql server database for product details(productID, ProductName,ProductPrice)
                foreach (DataRowView dr in dscrntProd.Rows)
                {
                    addToOrder(dr);
                }
            }
        }
 
Any help Most Appreciated
Thanks.
Posted 3-Jan-13 6:17am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

foreach (DataRowView dr in dscrntProd.Rows)
 
You cannot cast a DataRow into DataRowView.
Try foreach (DataRow dr in dscrntProd.Rows) instead.
  Permalink  
Comments
Marcus Kramer at 3-Jan-13 13:13pm
   
+5.
Abhinav S at 3-Jan-13 22:00pm
   
Thank you Marcus.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 295
1 OriginalGriff 285
2 Sergey Alexandrovich Kryukov 157
3 Afzaal Ahmad Zeeshan 154
4 Peter Leow 115
0 OriginalGriff 7,510
1 DamithSL 5,494
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100