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

  • Home
  • SEARCH
  • 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 6887817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:55:51+00:00 2026-05-27T05:55:51+00:00

I get InvalidCastException when I call First() at the last line. Can anyone tell

  • 0

I get InvalidCastException when I call First() at the last line. Can anyone tell me why?

static bool TableExists(string tableNameAndSchema) 
{
    string checkTable =
        String.Format(
        "IF OBJECT_ID('{0}', 'U') IS NOT NULL SELECT 'true' ELSE SELECT 'false'",
        tableNameAndSchema);            

    var result = db.ExecuteQuery<bool>(checkTable);
    return result.First();
}
  • 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-27T05:55:52+00:00Added an answer on May 27, 2026 at 5:55 am

    Both of the following two formats work properly. The core of the challenge appears to be what type is returned by ExecuteQuery.

    SELECT ‘true’ actually returns a string and not a bool – thus the ExecuteQuery result variable also has to be string (and not bool) if using that format

    And when using SELECT 1 as suggested by @Eben Roux and @Anders Abel, then the ExecuteQuery result variable has to be int (and not bool)

    static bool TableExists(string tableNameAndSchema)
    {
        string checkTable =
            String.Format(
            "IF OBJECT_ID('{0}', 'U') IS NOT NULL SELECT 'true' ELSE SELECT 'false'",
            tableNameAndSchema);
    
        IEnumerable<string> result = db.ExecuteQuery<string>(checkTable);
        return Convert.ToBoolean(result.First());
    }
    
    static bool TableExists2(string tableNameAndSchema)
    {
        string checkTable =
            String.Format(
            "IF OBJECT_ID('{0}', 'U') IS NOT NULL SELECT 1 ELSE SELECT 0",
            tableNameAndSchema);
    
        IEnumerable<int> result = db.ExecuteQuery<int>(checkTable);
        return Convert.ToBoolean(result.First());
    }
    

    Thanks to @Eben Roux and @Anders Abel whose answers pointed me toward the final solution(s) – (I marked your answers as useful as they steered me in the right direction to get the final answer – thanks guys 🙂 )

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

Sidebar

Related Questions

Given the following, why does the InvalidCastException get thrown? I can't see why it
I get an InvalidCastException converting a linq entity list to a businessobject list using
i get a valid fd object from a caller. How can i find out
Why do I get an InvalidCastException when trying to do this? throw (ArgumentNullException)(new Exception(errormessage,
Consider this code sample: public class Human { public string Value { get; set;}
see also Why do I get InvalidCastException from OperationContext.Current.GetCallbackChannel<>() I wish to pass my
Possible Duplicate: Foreach can throw an InvalidCastException? Consider the following block of code public
I just made my first converter to convert from int to string. I have
Here is the code: internal static void UpdateErrorLogTable(string type, string location, string method, Exception
get an array of bluetooth ID's Broadcast the bluetooth signal manually

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.