0

First of all: I know what causes this error and do not need a solution to fix the error but wonder why this happens.

I got this SQL-Procedure:

ALTER PROCEDURE GetStuff     
AS BEGIN
  SELECT
     id,
     anyValue,
     nutnullableValue
  FROM
     tableName
  UNION SELECT
     0,
     'Hey folks',
     NULL
END

I access this using LiNQ2SQL:

var data=_dataContext.GetStuff();
dta.DataSource=data;

("dta" is a DataGridView) When I use TargetType: "Any CPU" I receive an InvalidCastOperation at the last line which is correct, because the 2nd Select returns a NULL where a non-nullable Type is expected.

When I run the same Code as Target-CPU x86 no error (or at least no visible error) is thrown, but the datagridview just keeps being empty instead.

As told before: I know how to fix the error but am curious, why there is a different behavior when using another platform target CPU.

(update) Generated Code: The linq-Classes are created by the Standard-dbml-functionality of VS 2013:

public partial class GetStuffResult
    {
        private int _id;
        private string _anyValue;
        private int _notNullableValue;


        public GetStuffResult()
        {
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", DbType="Int NOT NULL")]
        public int id
        {
            get
            {
                return this._id;
            }
            set
            {
                if ((this._id != value))
                {
                    this._id = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_anyValue", DbType="NVarChar(MAX) NOT NULL", CanBeNull=false)]
        public string anyValue
        {
            get
            {
                return this._anyValue;
            }
            set
            {
                if ((this._anyValue != value))
                {
                    this._anyValue = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_notNullableValue", DbType="Int NOT NULL")]
        public int notNullableValue
        {
            get
            {
                return this._notNullableValue;
            }
            set
            {
                if ((this._notNullableValue != value))
                {
                    this._notNullableValue = value;
                }
            }
        }
    }

Data contains an element of type

[System.Data.Linq.SqlClient.SqlProvider.SingleResult<mynamespace.GetStuffResult>]

wich contains the folling error in the base - property:

"-{"cannot assign NULL to an element of type \"System.Int32\" which does not allow NULL."}  System.SystemException {System.InvalidOperationException}

(in both cases)

Ole Albers
  • 8,175
  • 8
  • 65
  • 145
  • Well, in the case where it doesn't throw an `InvalidCastException`, what does `data` end up holding? Heck: how is `data` *declared* (we can't see that); i.e. what does `_dataContext.GetStuff()` claim to return? and if it involves `SomeGeneratedType`, what does `SomeGeneratedType` look like? – Marc Gravell Feb 25 '14 at 12:27
  • @MarcGravell updated my question – Ole Albers Feb 25 '14 at 12:40

0 Answers0