C# – “The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.”

clinqwcf

I have a WCF service (services). The services manage the data. But now I have problems by selecting data in a LINQ-query. I get a message "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type."

By debugging the first time I walk through the method you'll finish to the exception-line. When I put back the debugger line (the yellow marked one) onto line "// 08", and then I walk through the method everything goes fine.

    private static ResultClass Geselecteerd(int AID_Artiest)  // 01
    { // 02
        ResultClass _Result = new ResultClass(); // 03
        string sMethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;// 04
        // 05
        try // 06
        { // 07
            var recArtiest = (from artiest in DatabaseControl.GetDatabeheer.DataContext.ARTIESTs  // 08
                           where artiest.ID_ARTIEST == AID_Artiest
                           select artiest).SingleOrDefault();

            if (recArtiest != null)
            {
                ArtiestDataType.ID_Artiest = recArtiest.ID_ARTIEST;
                ArtiestDataType.Artiestnaam = recArtiest.ARTIESTNAAM;
                ArtiestDataType.Voorvoegsel = recArtiest.VOORVOEGSEL;
                ArtiestDataType.Product = recArtiest.PRODUCT;
                ArtiestDataType.ID_Categorie = recArtiest.ID_CATEGORIE;
                ArtiestDataType.ID_Genre = recArtiest.ID_GENRE;
                ArtiestDataType.Is_Band = BasisDataType.GetBoolString(recArtiest.IS_BAND);
                ArtiestDataType.Land = recArtiest.LAND;
                ArtiestDataType.Plaats = recArtiest.PLAATS;
                ArtiestDataType.Website = recArtiest.WEBSITE;
                ArtiestDataType.DatumInvoer = recArtiest.DATUM_INVOER;
                ArtiestDataType.DatumMutatie = recArtiest.DATUM_MUTATIE;
            }
        }
        catch (Exception ex)
        {
            string sFoutmelding = "Kan geen selectie maken.";
            FLogboek.AddFoutmelding(FClassName, sMethodName, sFoutmelding, ex.Message);

            _Result.Code = ResultCode.FATAAL;
            _Result.Melding = sFoutmelding;
        }

        return _Result;
    }

ID_ARTIEST is an integer field and it is a primary key too. The value of the field is required and a NULL-value is not possible.

This method has always worked. The problem starts when I add new Service.

My question is: What do I wrong? Has is to do with the service? Do I have to delay the process?
Can anybody give me some tips? I've 8 service references. If you're missing some essential sources, I can add it.

I have now only a work-around.
I have added try-catch inside the try-catch area. And then I didn't get any exception. It has to do with the performance. Do I need an Async-solution?

Thanks.

Best Answer

Almost certainly what is happening is one of the properties of the type ARTIESTs in DatabaseControl.GetDatabeheer.DataContext.ARTIESTs is of type int? (which means nullable integer).

At a guess I would say it's either ID_CATEGORIE or ID_GENRE.

You will need to modify the equivalent property of your type ArtiestDataType to also be of type int?.

This will resolve your issue.