20

In this query I have 3 records (int-(telefon = d.telefon), decimal-(pesel = d.pesel), decimal-(nip = d.nip)) another records are strings.

public ActionResult detail(int LoginID)
{
    var user = (from d in baza.uzytkowniks
                where LoginID == d.LoginID
                select new uzytkownikModel {
                    imie = d.imie, 
                    nazwisko = d.nazwisko, 
                    telefon = d.telefon, 
                    pesel = d.pesel, 
                    nip = d.nip, 
                    email = d.email, 
                    adres_zamieszkania = d.adres_zamieszkania}).ToList();

    ViewBag.daneuser = user;
    return View();
}

And I have error:

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

and two another errors with 'decimal?' instead 'int?'.

Model:

 public class uzytkownikModel
    {
    [Required]
    public string imie { get; set; }

    [Required]
    public string nazwisko { get; set; }


    public decimal pesel { get; set; }
    public decimal nip { get; set; }

    public string adres_zamieszkania { get; set; }
    public int telefon { get; set; }
    public string email { get; set; }

}

Only imie and nazwisko are nonnullable, rest have allow null

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now work. I change in model:

   decimal to decimal?

and

int to int?

Thank you everyone for help

2
  • why you dont show uzytkownikModel class? probably you getting that error becouse of fields or properties doesnt match with same type with your values. Nov 8, 2011 at 16:27
  • can you post the definition of the type of 'd'?
    – JohnD
    Nov 8, 2011 at 16:30

4 Answers 4

41

You'll need to post more code to get a definitive answer, but somewhere one of your variables is nullable, and to assign it to a non-nullable type you need to do .Value.

For example, if your object d's property imie is Nullable<int>, and that is what is causing your problem, you could do this:

imie = d.imie.Value

You will also need to watch for cases where d.imie is null. For example:

imie = d.imie.HasValue ? d.imie.Value : 0

Or, if you like the ?? operator (which evaluates to the first non-null value):

imie = d.imie ?? 0

(I am just using d.imie as an example -- there is not enough code posted to pintpoint the exact problem.)

1
  • The d.imie.Value worked for me just like d.imie ?? 0; // Thanks a lot
    – Catto
    Nov 4, 2014 at 21:53
19

In your uzytkownikModel class, the properties that are nullable should be declared as "int?" or "decimal?" instead of "int" and "decimal".

4

int? is the Nullable<int>. Since database columns may be null, the mapped properties are Nullable.

Consider this code which assigns int? values to int variables.

int? w = 2;
int? x = null;
int y = w ?? 3;
int z = x ?? 4;
0

The following will work:

int ID = (int)(i.ID == null ? 0 : i.ID);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.