Cannot implicitly convert type 'int?' to 'int' - either on Controller or on View Page

2016-02-15 c# entity-framework asp.net-mvc-4

While executing below code, I got this error in my Controller:

Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)

Error comes on t.TOT_QTY in my controller.

Controller:

dcms = (from t in db.ASN_ITEM
                    ...
                    where
                      (t.TOT_QTY - t.SCND_QTY) != 0  
                    select new ASNVarianceRep
                    {                           
                        TOT_QTY = t.TOT_QTY,
                        SCND_QTY = t.SCND_QTY,
                        VAR_QTY = (t.SCND_QTY - t.TOT_QTY)                            
                    }).Distinct();   

Where t.TOT_QTY is of type int? in the database. But the variable TOT_QTY is just an int on Model class.

Model:

public class ASNVarianceRep : ASN_ITEM
{  
    public int TOT_QTY { get; set; }
    ...
 } 

If I changed public int TOT_QTY { get; set; } to public Nullable<int> TOT_QTY { get; set; }, Then error in the controller will be solved. But same error will be there on view page at line itotqty = itotqty + item.TOT_QTY;

View:

 @if (Model != null)
    {
        var itotqty = 0;
        foreach (var item in Model)
        {
            itotqty = itotqty + item.TOT_QTY;                
        <tr>                
            <td>
                @Html.DisplayFor(modelItem => item.TOT_QTY)
            </td>
            ...
        </tr>
        }
        <tr>                
            <td style="text-align: center"><b>@itotqty.ToString() </b></td>                
        </tr>

So, what I want is either I need to make the type of t.TOT_QTY from int? To int in Controller or make var itotqty = 0; from int to int? on view page. How to do so?

Answers

You can explicitly cast to an int in your query when assigning to your model. Currently it is trying to do an implicit cast as failing.

dcms = (from t in db.ASN_ITEM
       ...
       where
           (t.TOT_QTY - t.SCND_QTY) != 0  
           select new ASNVarianceRep
           {                           
               TOT_QTY = (int)t.TOT_QTY,
               SCND_QTY = t.SCND_QTY,
               VAR_QTY = (t.SCND_QTY - t.TOT_QTY)                            
           }).Distinct();   

See this MSDN page for more info on explicit casting: https://msdn.microsoft.com/en-us/library/ms173105.aspx

Leave your changes in the model and then in your view

 totqty = itotqty + Convert.ToInt32(item.TOT_QTY);

int? and int are basically two different types that you can't implicitly cast to, so you need to do an explicit cast. Explicit casts look like this:

int  myInt  = 3;
byte myByte = (Byte)myInt;

Where the (Byte) casts the int to a byte. It does this by create a new variable, preforming a hidden operation, then returning that value, much like a function.

To do that with your example, it would look like this:

dcms = (from t in db.ASN_ITEM
   ...
   where
       (t.TOT_QTY - t.SCND_QTY) != 0  
       select new ASNVarianceRep
       {                           
           TOT_QTY = (int)t.TOT_QTY,
           SCND_QTY = t.SCND_QTY,
           VAR_QTY = (t.SCND_QTY - t.TOT_QTY)                            
       }).Distinct();   

But keep in mind that when you cast from a Type? to Type there is always a chance of memory loss, in the event you convert a null value to an int. I'm not sure what what null value is cast to, maybe 0, but point is, you have lost that data.

I would suggest you to keep your model property nullable

public Nullable<int> TOT_QTY { get; set; }

or

public int? TOT_QTY { get; set; }

and handle it in your view

itotqty = itotqty + (int)(item.TOT_QTY ?? 0);

You could also convert a nullable int to int by the following

int temp= t.TOT_QTY ?? default(int);

Related