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

The Archive Base Latest Questions

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

How come I get an invalid cast exception when trying to set a NULL

  • 0

How come I get an invalid cast exception when trying to set a NULL value returned from the database inside Comments which is of type Int32.

I am trying to replace this:

                try
                {
                    objStreamItem.Comments = (Int32)sqlReader["Comments"];
                    if (objStreamItem.Comments > 0) { 
                        listComments = Comment.GetAll(objStreamItem.Id);

                    }
                }
                catch (InvalidCastException)
                {
                    // Execute if "Comments" returns NULL
                    listComments = null;
                    objStreamItem.Comments = 0;
                }

With this:

Comments = ((Int32?)sqlReader["Comments"]) ?? 0

The two are in different contexts, but you should get the idea. Instead of using a try catch block I am trying to solve this in a more elegant way.

Thanks.

UPDATE

It is stored as a nullable integer in the database.

    public int? Comments
    {
        get;
        set;
    }

I just want to thank everyone who answered or posted on this question, everything was very informative. thanks!

  • 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-27T18:42:30+00:00Added an answer on May 27, 2026 at 6:42 pm

    I suppose a ternary expression is the way to go here

    objStreamItem.Comments = sqlReader["Comments"] is DBNull ? 0 : (Int32)sqlReader["Comments"] ;
    

    You could also store the return value of sqlReader["Comments"] in a variable first to shorten the expression

    var comments = sqlReader["Comments"];
    objStreamItem.Comments = comments is DBNull ? 0 : (Int32)comments;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to come up with a way to get data from a SQL
How come I get this message from Firefox Web Console The Web Console logging
Please Help me.I am trying to over come this paoblem from last 2 days
I get the following unhandled exception thrown from my web service - this isn't
I'm targeting .NET 3.5, but cannot get System.Data to come up in Intellisense. What
Come across what looks at first sight like an MT-issue, but I'm trying to
I come from a Java background, where packages are used, not namespaces. I'm used
I come from the Microsoft world (and I come in peace). I want to
I come from a .NET world and I'm new to writting C++. I'm just
I get this error occasionally: Fatal error: Uncaught Exception: 190: Error validating access token:

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.