Khurri.Saleem

If you define a table with nullable fields, like:

CREATE TABLE [MigrationData].[LinqTest](
[Id] [int] IDENTITY(1,1) NOT NULL,
[stringValue] [nvarchar](50) NULL,
[intValue] [int] NULL,
[byteValue] [tinyint] NULL,
CONSTRAINT [PK_LinqTest] PRIMARY KEY CLUSTERED ( [Id] ASC)
)

... and you execute in a datacontext the following:

cibleDC.ExecuteCommand("UPDATE MigrationData.LinqTest SET stringValue={0}, intValue={1}, byteValue={2} WHERE Id=1", "A", 1, 1);

all is fine. But the following:

cibleDC.ExecuteCommand("UPDATE MigrationData.LinqTest SET stringValue={0}, intValue={1}, byteValue={2} WHERE Id=1", null, 1, 1);
cibleDC.ExecuteCommand("UPDATE MigrationData.LinqTest SET stringValue={0}, intValue={1}, byteValue={2} WHERE Id=1", "A", null, 1);

... fail with "NotSupportedException": A query parameter cannot be of the type System.Object.
Casting null to the appropriate type doesn't make any difference.
I did not test the effect of Nullable types.

However in the documentation you find the following:

? If any of the parameters is null, it is converted to DBNull.Value

So the command does not behave according to the documentation. At least one of them is wrong <g>.



Re: LINQ Project General A query parameter cannot be of the type System.Object

Khurri.Saleem

can anyone reply to it please Smile





Re: LINQ Project General A query parameter cannot be of the type System.Object

Jason Cooke - MSFT

The documentation is incorrect. The ExecuteCommand method does not support null parameters. I will file a bug against the help topic.

You can work around this issue by creating a helper method like this:

private void UpdateHelper(int intValue) {

if (intValue.HasValue)

cibleDC.ExecuteCommand("UPDATE MigrationData.LinqTest SET stringValue={0}, intValue={1}, byteValue={2} WHERE Id=1", "A", intValue, 1);

else

cibleDC.ExecuteCommand("UPDATE MigrationData.LinqTest SET stringValue={0}, intValue=NULL, byteValue={1} WHERE Id=1", "A", 1);

}

Hope that helps,

Jason






Re: LINQ Project General A query parameter cannot be of the type System.Object

Khurri.Saleem

But this means if there are more than one NULLable values you need to have multiples IF conditions.

i think this should not be a documentation bug rather it would be more like a framework bug. Framework should convert nullable values to DB NULL automatically as it does with LINQ Updation.