I try to multiply 2 values from different table but it's keep error like this:

Imgur

Here's my code:

    //Label12 is in datalist
    var Label12 = (Label)(((Button)sender).Parent).FindControl("Label12");  

    var item2 = from c in db.OrderTemps
                join o in db.Products
                on c.Prod_ID equals o.Prod_ID
                select new
                {
                   o.Prod_ID,
                   o.Prod_Price, //Type "Decimal"
                   c.Amount, //Type "Integer"
                   c.Total, //Type "Decimal
                   s = o.Prod_Price * c.Amount
                };

     foreach (var i in item2)
     {
        Label12.Text = Convert.ToString(i.s); //an error happen here!!
     }

Any help appreciated.

有帮助吗?

解决方案

There is nothing wrong with you LINQ query. If no records joined, then there will be nothing to enumerate and you will skip foreach body. I think you have Label12 equal to null. Check if it really exist on senders parent.

SIDE NOTE: Why are you trying to assign text in loop? Label will keep only last value assigned.

其他提示

Check whether your amount value in OrderTemps table is null. or Check whether you really have a controller which has the ID = Label12. Nothing wrong in your Linq query...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top