Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7500219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:06:27+00:00 2026-05-29T20:06:27+00:00

So my problem is this. When using the Data Context ExecuteQuery(string query, params object[]

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:06:28+00:00Added an answer on May 29, 2026 at 8:06 pm

    You could do it something like this:

    db.ExecuteQuery<UpInsertTest4Result>(string.Format(query,1,"null"));
    

    Update

    Yes I have tested the solution. It will not contain @p1=N'null'. Because I am using sql as a string directly. So a prof of concept.

    Store procedure:

    CREATE PROCEDURE testas
    @test1 INT,  
    @test2 INT 
    AS
    SELECT 1 test 
    

    class for testing the output:

    public class testout
    {
        public int test { get; set; }
    }
    

    Linq query with the db context:

    var query="exec testas {0}, {1}";
    db.ExecuteQuery<testout>(string.Format(query,"0","null"));
    

    If you look in linqpad the sql output is like this:

    exec testas 0, null
    

    The string format is just altering the string. This would be the same as writing this:

    db.ExecuteQuery<testout>("exec testas 0, null")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem pertains to Java By using RandomAccessFile I intend to be able to
I think that this problem can be sorted using reflection (a technology which I'm
My problem is this one, I am using Sharepoint 2010, I have a form
I remember running into this problem when I started using OpenGL in OS X.
I periodically get this problem where all of a sudden mako is using old
I am using MS AJAX ASP.NET Components (Calendar Extender) and I'm finding this problem.
I'm using Glade-3 for my GUI design, but I keep hitting this problem. I
We are currently using JDeveloper to build our production EARs. One problem with this
I have a problem - i can't compile SqlCipher. I'm using this http://groups.google.com/group/sqlcipher/browse_thread/thread/55c6296b56bf4533/c792bbec6df7d4f4?tvc=2#c792bbec6df7d4f4 instructions
I'm using django with app-engine-patch and I'm having this wierd problem running manage.py dumpdata

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.