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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:29:31+00:00 2026-06-11T19:29:31+00:00

EnumType = reader[EnumTypeId] == DBNull.Value ? EnumType.None : (EnumType)(int)reader[EnumTypeId]; I thought if reader[EnumTypeId] is

  • 0
EnumType = reader["EnumTypeId"] == DBNull.Value ? EnumType.None : (EnumType)(int)reader["EnumTypeId"];

I thought if reader["EnumTypeId"] is null, it should assign the EnumType.None value, but it is still trying to cast the null value to an int which is obviously causing an exception.

I tried the following and it did not work either:

EnumType = reader["EnumTypeId"] == null ? EnumType.None : (EnumType)(int)reader["EnumTypeId"];

Instead of using Enums, I went ahead and decided to use a nullable int, so now my code is slightly different, but it still does not work with DBNull.Value, null, or GetOrdinal...

intType= reader["intType"] == DBNull.Value ? null : (int?)reader["intType"];

Also, why do I have to do a (int?) cast instead of just a (int) cast?

  • 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-11T19:29:32+00:00Added an answer on June 11, 2026 at 7:29 pm

    Don’t use DBNull, just use plain old null.

    EnumType = reader["EnumTypeId"] == null ? EnumType.None : (EnumType)(int)reader["EnumTypeId"];
    

    Edit

    The issue could be that the database type of EnumTypeId isn’t an int/Int32. If so, then reading as a string and then parsing should fix the problem.

    EnumType? enumVal = null;
    if (reader["EnumTypeId"] != null)
    {
        int intVal;
        enumVal = (int.TryParse(reader["EnumTypeId"].ToString(), out intVal)) ? (EnumType)intVal : null;
    }
    EnumType =  ? EnumType.None : (EnumType)(int)reader["EnumTypeId"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following enumeration: [System.Flags] public enum EnumType: int { None = 0, Black
class Program { static void Main(string[] args) { string value = "12345"; Type enumType
I have this code @Column(updatable=false) @Enumerated(EnumType.STRING) private ExamType examType; However, I can still change
** MAJOR UPDATE ** I made a minor mistake but I'm still curious about
class Program { static void Main(string[] args) { String value = Two; Type enumType
I am retrieving columns of different types and checking for null before assigning the
I have an enum type defined: EnumType Now imagine object A = EnumType.Value1; object
I'm writing a method which determines the highest value in a .NET enumeration so
I think this question needs some code: private TypeValues GetEnumValues(Type enumType, string description) {
I would like to implement a usercontrol which gets a enumtype via XAML code.

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.