Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, July 17, 2018 1:39 AM
I am doing [this] tutorial abut barcode generation but this error showed. I did what was in the tutorial but why does an error showed? I am using MVC 5.2.3.0.
foreach (DataSet1.PrintDivRow row in dt.Rows)
{
bcp.Code = row.AppCode.ToString();
row.Barcode = bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png);
}
the error is found here
foreach (DataSet1.PrintDivRow row in dt.Rows)
Thanks
All replies (5)
Tuesday, July 17, 2018 7:13 AM âś…Answered
Unable to cast object of type 'System.Data.DataRow' to type 'PrintDivRow'.
A class is instanced into an object, and a class defines the object, which is the blueprint. You can't tell a object/type it's going to be another object/type, becuase they have different blueprints.
foreach (DataSet1.PrintDivRow row in dt.Rows)
The object/type in dt.Rows is a System.Data.DataRow object/type, and it is not a PrintDivRow object/type, with both objects/types having their own class/blueprint.
It's OOP 101 no matter the language platform being used.
https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/
foreach (DataRow row in dt.Rows)
foreach (var row in dt.Rows)
The var would tell the compiler to make it a DataRow, strong type it, without you defining it, because it knows dt.Rows will have DataRow objects in it.
Wednesday, July 18, 2018 12:45 AM
Thanks for the reply sir. Ok so I have to change row into a DataRow the problem now is why can't the system find my newly added column in the DataSet?
foreach (DataRow row in dt.Tables[0].Rows)
{
bcp.Code = row["appcode"].ToString();
row["Barcode"] = bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png);
}
the error now is this, Column 'Barcode' does not belong to table PrintDiv.
Wednesday, July 18, 2018 3:55 AM
Thanks for the reply sir. Ok so I have to change row into a DataRow but if I will do it, how will I call the DataSet column so that it could be populated? Thanks sr
A datatable has 1 or more rows and each row has 1 or more columns. A dataset is a container that holds 1 or more datatables. A dataset does not contain columns. What the PrintDivRow is about, I have no idea as to what that is about, which I can't even get a hit on using Google.
How did you come up with 'PrintDivRow' what is it?
Wednesday, July 18, 2018 4:13 AM
One other thing, you are doing some example using VS2005 with MS SQL Server with Crystal Reports, which is questionable in itself. Did you meet all the requirements to even make it work?
Wednesday, July 18, 2018 5:44 AM
The PrintDivRow is the DataTable in DataSet from the link I attached in my post, I just did what was in the tutorial.
I retreated to your answer earlier and tried to search more. Now I made fully working. Thanks for the answer again. Appreciate it