Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top