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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:23:34+00:00 2026-05-11T00:23:34+00:00

I have a database that hold’s a user’s optional profile. In the profile I

  • 0

I have a database that hold’s a user’s optional profile. In the profile I have strings, char (for M or F) and ints.

I ran into an issue where I try to put the sex of the user into the property of my Profile object, and the application crashes because it doesn’t know how to handle a returned null value.

I’ve tried casting the data to the appropriate type

char sex = (char)dt.Rows[0]['Sex']; 

Which didn’t fix my problem. I then tried changing the types to Nullable and Nullable and get conversion issues all the same. My current solution that I was able to find is the following:

object.sex = null;   if(dt.Rows[0]['Sex'] != DBNull.Value)       object.sex = (char)dt.Rows[0]['Sex']; object.WorkExt = null; if(dt.Rows[0]['WorkExt'] != DBNull.Value)       object.WorkExt = (int)dt.Rows[0]['WorkExt']; 

Is there a simpler or better way to do this? Or am I pretty much on the right track?

  • 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. 2026-05-11T00:23:34+00:00Added an answer on May 11, 2026 at 12:23 am

    rotard’s answer (use Is<ColumnName>Null()) only works for typed data sets.

    For untyped data sets, you have to use one of the patterns in the following code. If this code isn’t definitive, let me know and I’ll edit it until it is. This is an extremely common question that there should really be only one right answer to.

    using System. using System.Data;  class Program {     static void Main(string[] args)     {         DataTable dt = new DataTable();         dt.Columns.Add('test', typeof (char));         dt.Columns['test'].AllowDBNull = true;          DataRow dr = dt.Rows.Add();         char? test;          try         {             test = (char?)dr['test'];         }         catch (InvalidCastException)         {             Console.WriteLine('Simply casting to a nullable type doesn't work.');         }          test  = dr.Field<char?>('test');         if (test == null)         {             Console.WriteLine('The Field extension method in .NET 3.5 converts System.DBNull to null.');                         }          test = (dr['test'] is DBNull) ? null : (char?) dr['test'];         if (test == null)         {             Console.WriteLine('Before .NET 3.5, you have to check the type of the column's value.');         }          test = (dr['test'] == DBNull.Value) ? null : (char?) dr['test'];         if (test == null)         {             Console.WriteLine('Comparing the field's value to DBNull.Value is very marginally faster, but takes a bit more code.');         }          // now let's put the data back          try         {             dr['test'] = test;         }         catch (ArgumentException)         {             Console.WriteLine('You can't set nullable columns to null.');         }          dr.SetField('test', test);         if (dr['test'] is DBNull)         {             Console.WriteLine('Again, in .NET 3.5 extension methods make this relatively easy.');         }          dr['test'] = (object)test ?? DBNull.Value;         if (dr['test'] is DBNull)         {             Console.WriteLine('Before .NET 3.5, you can use the null coalescing operator, but note the awful cast required.');         }           Console.ReadLine();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I don't know of a way to do the overlay… May 11, 2026 at 9:55 am
  • added an answer Some further research and I found this: INFO: List of… May 11, 2026 at 9:55 am
  • added an answer Microsoft Software Licensing and Protection Services has functionality to bind… May 11, 2026 at 9:55 am

Related Questions

I have a database that hold's a user's optional profile. In the profile I
I currently have a SQL Server (Express 2005) database to hold some transaction/metadata that
I have a database that contains a date and we are using the MaskedEditExtender
I have a database that I would like class files generated from, and also
I have a database that many different client applications (a smattering of web services,
I have a database that contains a table that looks a bit like this:
I have a database that i'm dealing with which is updated every few hours
I have a database that is stuck in single-user mode. I kill the process
I have a database that I used specifically for logging actions of users. The
I have a database that was restored from a SQL 2000 instance to a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.