"The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type."

13,304

Solution 1

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.

Solution 2

What do I wrong?

Except assigning null to a non-nullable element (a struct)?

Your code is meaningless (posting a ton is not as good as actually posting the relevant parts, which you miss).

I could be that the field in the database is nullable and CONTAINS null, but your field is not int? (nullable of int) so it can not assign null there.

I would look in the direction of a database mapped table being wrong. Check fields, check that all nullable columns actually are nullable in the class.

Solution 3

Change

where artiest.ID_ARTIEST == AID_Artiest

to

where artiest.ID_ARTIEST.HasValue && artiest.ID_ARTIEST.Value == AID_Artiest

One or more of your artists don't have an ID_ARTIEST set (which you should fix, it does look like a primary key which should always be present), so when comparing (int?)null == int the exception is thrown.

Share:
13,304
user1531040
Author by

user1531040

Updated on June 24, 2022

Comments

  • user1531040
    user1531040 almost 2 years

    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.