Avatar of Kep Deep
Kep Deep
 asked on

Can anyone help me fix a InvalidCastException on VS windows forum C#

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'.

        private void listgames_Click(object sender, EventArgs e)
        {
            DataRow drgameshop;
            int rowNo;

            rowNo = Convert.ToInt32(lstgames.SelectedValue);   //THIS IS WHERE THE ERROR OCCURS

            drgameshop = dscategory.Tables["Category"].Rows.Find(rowNo);

            lblCN.Text = drgameshop["Category.CategoryNo"].ToString();
            lblSN.Text = drgameshop["Stock.StockNo"].ToString();
            lblSD.Text = drgameshop["StockDesc"].ToString();
            lblQ.Text = drgameshop["Qty"].ToString();
            lblCP.Text = drgameshop["CostPrice"].ToString();
            lblSP.Text = drgameshop["SellingPrice"].ToString();
            lblRL.Text = drgameshop["ReOrderLevel"].ToString();
            lblRQ.Text = drgameshop["ReOrderQty"].ToString();
            lblSupNo.Text = drgameshop["Supplier.SupplierNo"].ToString();
            lblON.Text = drgameshop["OrderDetail.Orderno"].ToString();
            lblQO.Text = drgameshop["QtyOrdered"].ToString();
        }
    }
}

Open in new window

C#Microsoft Visual Studio

Avatar of undefined
Last Comment
sarabande

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kep Deep

ASKER
Thanks for the responses. I managed to fix the problem "kinda". I was using too many tables just when i should of been using one table for this forum. Its working fine now.
Fernando Soto

That is great, have a nice day.
sarabande

The solution was not more than to explain the error message and give a hint that the type of the variable listgames might be wrong. As the Author was able to solve the issue by pursuing the hints,  the question can be normally closed with an equal split.

Sara
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23