Can't figure out why this won't work. I get the same "Specified Cast is Invalid" error message. New to C#, be kind. It fails at the if(!((int)WrkRow["ManualWeight"] == 1 | etc. line. I tried a few variations of code, not all pasted here. ManualWeight is a number field in the table.
if (dt.Rows.Count > 0)
{
DataRow WrkRow = dt.Rows[0]; // ds.Tables(0).Rows(0)
if (mod1.IsTareout == true)
trim = (string)WrkRow["Trucker"];
sBarcode = $"{trim.Trim()}{(string)WrkRow["TruckNo"]} ";
XRSwatLaserCert rSwatLaserCert = new XRSwatLaserCert();
rSwatLaserCert.DataSource = dt;
DevExpress.XtraReports.UI.ReportPrintTool rpt = new DevExpress.XtraReports.UI.ReportPrintTool(rSwatLaserCert);
{
XRBarCode XrBCTareOut = new XRBarCode();
rSwatLaserCert.XrBCTareOut = new XRBarCode
{
Text = sBarcode
};
if (!((int)WrkRow["ManualWeight"] == 1 | (int)WrkRow["ManualWeight"] == 3))
{
rSwatLaserCert.XrLabelManualGross1.Visible = false;
rSwatLaserCert.XrLabelManualGross2.Visible = false;
rSwatLaserCert.XrLabelManualGross3.Visible = false;
}
2nd Try:
if (dt.Rows.Count > 0)
{
DataRow WrkRow = dt.Rows[0]; // ds.Tables(0).Rows(0)
if (mod1.IsTareout == true)
trim = (string)WrkRow["Trucker"];
sBarcode = $"{trim.Trim()}{(string)WrkRow["TruckNo"]} ";
XRSwatLaserCert rSwatLaserCert = new XRSwatLaserCert();
rSwatLaserCert.DataSource = dt;
DevExpress.XtraReports.UI.ReportPrintTool rpt = new DevExpress.XtraReports.UI.ReportPrintTool(rSwatLaserCert);
{
XRBarCode XrBCTareOut = new XRBarCode();
rSwatLaserCert.XrBCTareOut = new XRBarCode
{
Text = sBarcode
};
if (WrkRow.Field<int>("ManualWeight") != 1 | (int)WrkRow.Field<int>("ManualWeight") != 3)
{
rSwatLaserCert.XrLabelManualGross1.Visible = false;
rSwatLaserCert.XrLabelManualGross2.Visible = false;
rSwatLaserCert.XrLabelManualGross3.Visible = false;
}
3rd try:
if (dt.Rows.Count > 0)
{
DataRow WrkRow = dt.Rows[0]; // ds.Tables(0).Rows(0)
if (mod1.IsTareout == true)
trim = (string)WrkRow["Trucker"];
sBarcode = $"{trim.Trim()}{(string)WrkRow["TruckNo"]} ";
XRSwatLaserCert rSwatLaserCert = new XRSwatLaserCert();
rSwatLaserCert.DataSource = dt;
ReportPrintTool rpt = new ReportPrintTool(rSwatLaserCert);
{
XRBarCode XrBCTareOut = new XRBarCode();
rSwatLaserCert.XrBCTareOut = new XRBarCode
{
Text = sBarcode
};
var manweight = WrkRow.Field<int>("ManualWeight");
if (manweight != 1 | manweight == 3)
{
rSwatLaserCert.XrLabelManualGross1.Visible = false;
rSwatLaserCert.XrLabelManualGross2.Visible = false;
rSwatLaserCert.XrLabelManualGross3.Visible = false;
}





WrkRow["ManualWeight"]is not anint. The data in controls like that are allobjects, so Value Type instances (likeints) are boxed. When you unbox a value, you need to unbox it to exactly the right type. If it's along(like10L) or adecimal(like10m), you can't just cast it to an intnumberdata type; that sounds like a database thing-we need to know the c# type it's mapped as. If you created the column manually, Show us thedt.Columns.Add("ManualWeight", ...)or if the datatable was created by filling from a database source, show us the result of callingdt.Columns["ManualWeight"].DataTypein the immediate window during a debug sessiondt.Columns["ManualWeight"].DataTypeinto the Immediate window, and press return, wait a short moment and read the garbage.. It'll be something likeblah blah System.Decimal blah blahwhich will mean you need to do(decimal)WrkRow["ManualWeight"]rather than(int)WrkRow["ManualWeight"]- when you've got a primitive inside anobjectyou need to get out using an exact cast to what it truly is. If, e.g, it was adecimalinside anobjectand you wanted it as anint, you'd actually have to do(int)(decimal)WrkRow["ManualWeight"]