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 6638491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:26:31+00:00 2026-05-25T23:26:31+00:00

Here’s the setup: I have an Open Source project called Massive and I’m slinging

  • 0

Here’s the setup: I have an Open Source project called Massive and I’m slinging around dynamics as a way of creating SQL on the fly, and dynamic result sets on the fly.

To do the database end of things I’m using System.Data.Common and the ProviderFactory stuff. Here’s a sample that works just fine (it’s static so you can run in a Console):

    static DbCommand CreateCommand(string sql) {
        return DbProviderFactories.GetFactory("System.Data.SqlClient")
                                  .CreateCommand();
    }
    static DbConnection OpenConnection() {
        return DbProviderFactories.GetFactory("System.Data.SqlClient")
                                  .CreateConnection();
    }
    public static dynamic DynamicWeirdness() {
        using (var conn = OpenConnection()) {
            var cmd = CreateCommand("SELECT * FROM Products");
            cmd.Connection = conn;
        }
        Console.WriteLine("It worked!");
        Console.Read();
        return null;
    }

The result of running this code is “It worked!”

Now, if I change the string argument to dynamic – specifically an ExpandoObject (pretend that there’s a routine somewhere that crunches the Expando into SQL) – a weird error is thrown. Here’s the code:

Dynamic Error

What worked before now fails with a message that makes no sense. A SqlConnection is a DbConnection – moreover if you mouseover the code in debug, you can see that the types are all SQL types. “conn” is a SqlConnection, “cmd” is a SqlCommand.

This error makes utterly no sense – but more importantly it’s cause by the presence of an ExpandoObject that doesn’t touch any of the implementation code. The differences between the two routines are:
1 – I’ve changed the argument in CreateCommand() to accept “dynamic” instead of string
2 – I’ve created an ExpandoObject and set a property.

It gets weirder.

If simply use a string instead of the ExpandoObject – it all works just fine!

    //THIS WORKS
    static DbCommand CreateCommand(dynamic item) {
        return DbProviderFactories.GetFactory("System.Data.SqlClient").CreateCommand();
    }
    static DbConnection OpenConnection() {
        return DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection();
    }
    public static dynamic DynamicWeirdness() {
        dynamic ex = new ExpandoObject();
        ex.TableName = "Products";
        using (var conn = OpenConnection()) {
            //use a string instead of the Expando
            var cmd = CreateCommand("HI THERE");
            cmd.Connection = conn;
        }
        Console.WriteLine("It worked!");
        Console.Read();
        return null;
    }

If I swap out the argument for CreateCommand() to be my ExpandoObject (“ex”) – it causes all of the code to be a “dynamic expression” which is evaluated at runtime.

It appears that the runtime evaluation of this code is different than compile-time evaluation… which makes no sense.

**EDIT: I should add here that if I hard-code everything to use SqlConnection and SqlCommand explicitly, it works 🙂 – here’s an image of what I mean:

enter image description here

  • 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-25T23:26:31+00:00Added an answer on May 25, 2026 at 11:26 pm

    When you pass the dynamic to CreateCommand, the compiler is treating its return type as a dynamic that it has to resolve at runtime. Unfortunately, you’re hitting some oddities between that resolver and the C# language. Fortunately, it’s easy to work around by removing your use of var forcing the compiler to do what you expect:

    public static dynamic DynamicWeirdness() {
        dynamic ex = new ExpandoObject ();
        ex.Query = "SELECT * FROM Products";
        using (var conn = OpenConnection()) {
            DbCommand cmd = CreateCommand(ex); // <-- DON'T USE VAR
            cmd.Connection = conn;
        }
        Console.WriteLine("It worked!");
        Console.Read();
        return null;
    }
    

    This has been tested on Mono 2.10.5, but I’m sure it works with MS too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the basic setup: I have a thin bar at the top of a
Here is an example: I have the generic type called Account. I wish to
Here is my situation: I have a controller action called OrderFromCategory which fills a
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here we go again, the old argument still arises... Would we better have a
Here is my problem : I have a post controller with the action create.
Here is my code...I have two dimensional matrices A,B. I want to develop the
here's what we have today: * NxM grid of points in 3D * we

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.