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 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

Related Questions

I have a database table like that :- Table1 Column1 : Varchar(50) Column2 :
I have a set of categories for a MySQL database that wont change much,
I'm designing a MySQL database and a corresponding RoR app that will hold various
heres the problem, i have a database thats hold information on marker images: i'm
I have been making a database and learning along the way. I recently got
I'm looking for some guidance as to how to set up a database I
Is it possible to store more than one value and query the database and
I recently redesigned my site, complete with a new database, but I wanted to
Short version : How can I write an SQL procedure to list which of
I am doing a project on criminal tracking using gps and i am not

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.