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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:59:35+00:00 2026-06-18T00:59:35+00:00

I am getting Specified cast is not valid error in this method public Dictionary<byte,

  • 0

I am getting Specified cast is not valid error in this method

public Dictionary<byte, string> getTeamList(int id1, Dictionary<byte, int> myList)
{    
    Dictionary<byte, string> result = new Dictionary<byte, string>();
    var query= _dataContext.usp_getItem();
    foreach(var item in query)
    {
        if (myList.ContainsKey(item.ID)
        {
            int count= _dataContext.usp_getCountPerID(id1, item.ID).FirstOrDefault().Count;
            if (myList[item.ID] > count)
            {
                result.Add(item.ID, item.Name);
            }
        }
        else
        {
            result.Add(item.ID, item.Name);
        }
    }
    return result;
} 

I’m getting the InvalidCastException error at this line:

int count = _dataContext.usp_getCountPerID(item.ID).FirstOrDefault().Count;

The type of Count is int. It works when I comment out this line of code.

UPDATE

usp_getCountPerID looks like this:

@ID1 INT,
@ID2 TINYINT
SELECT COUNT(ID) AS Count  //I'm changing this name so it's actually not "Count"
FROM   Table
WHERE  ID1 = @ID1
       AND ID2 = @ID2
  • 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-06-18T00:59:36+00:00Added an answer on June 18, 2026 at 12:59 am

    It may not be the best solution but I changed my stored procedure to get count to this:

    @SessionID INT
    SELECT ID, COUNT(Member) AS Count
    FROM   Table
    WHERE  SessionID = @SessionID 
    GROUP BY ID
    

    which returns a table containing ID and the total count of Members for each ID

    So, instead of getting values for the count per ID inside the foreach loop, I retrieve the list of ID and Count before entering the foreach loop and this works..

    I still don’t understand why I was getting the casting error, so I would mark it as answer if someone could explain what I was missing.

    Thank you so much for all your help.

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

Sidebar

Related Questions

I am intermittently getting an System.InvalidCastException: Specified cast is not valid. error in my
In my LINQ-to-SQL DAL, I'm getting a Specified Cast Was Not Valid. Here is
I am getting the following error C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': 126: The specified module could not
I am getting exception: Specific cast is not valid, here is the code con.Open();
I have this method: public T GetInput<T>() { T result; if( (typeof)T == Type.GetType(string))
I'm getting this error when trying to build: The entitlements specified in your application’s
I am getting the error specified in the question title for the following code
UPDATE: I tried implementing the method specified by Peter and am getting incorrect shadowing.
I keep on getting this error error: expected specifier-qualifier-list for core data code im
I had this SSIS package working yesterday and now I'm getting this error with

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.