So my problem is this. When using the Data Context ExecuteQuery(string query, params object[] parameters) (here), I am unable to pass through a null in any way shape or form. The parameter that started this off was a SQL nvarchar parameter, which needs to allow nulls.
I read this article on stackoverflow which had "use DBNull.Value" marked as the answer - so I tried that, and nothing (so wondering how that can be the answer, if it doesn't work??!).
Here is the sample code I am trying to run (note, this is just to test the concept):
var db = new Test1DataContext(Properties.Settings.Default.TestConnectionString);
var query = "EXEC UpInsertTest4 {0}, {1}";
// Works fine
var list1 = new List<object> { 1, "1" };
db.ExecuteQuery<UpInsertTest4Result>(query, list1.ToArray());
// Doesn't work
var list2 = new List<object> { 1, DBNull.Value };
db.ExecuteQuery<UpInsertTest4Result>(query, list2.ToArray());
Here are the various errors I receive :
null : A query parameter cannot be of type 'System.Object'.
default(string) : A query parameter cannot be of type 'System.Object'.
DBNull.Value : Unexpected type code: DBNull (which give this as a stack trrace)
at System.Data.Linq.SqlClient.SqlTypeSystem.Sql2005Provider.From(Type type, Nullable`1 size)
at System.Data.Linq.SqlClient.SqlTypeSystem.Sql2008Provider.From(Type type, Nullable`1 size)
at System.Data.Linq.SqlClient.SqlTypeSystem.ProviderBase.From(Type type)
at System.Data.Linq.SqlClient.SqlTypeSystem.ProviderBase.From(Object o)
at System.Data.Linq.SqlClient.SqlFactory.ValueFromObject(Object value, Type clrType, Boolean isClientSpecified, Expression sourceExpression)
at System.Data.Linq.SqlClient.QueryConverter.VisitConstant(ConstantExpression cons)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
at System.Data.Linq.SqlClient.QueryConverter.VisitUserQuery(String query, Expression[] arguments, Type resultType)
at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataContext.ExecuteMethodCall(Object instance, MethodInfo methodInfo, Object[] parameters)
at System.Data.Linq.DataContext.ExecuteQuery[TResult](String query, Object[] parameters)
at ConsoleApplication1.Program.Main(String[] args) in E:\TestBed\Testbed\ConsoleApplication1\Program.cs:line 36
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
From what I've read, I could be fighting a loosing battle with this! This is my last ditch attempt!
Many thanks,
Chris.