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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:00:06+00:00 2026-05-18T07:00:06+00:00

This seems to work for me, but was wondering if there are any issues

  • 0

This seems to work for me, but was wondering if there are any issues with it

 class Test
    {
        public int PKId { get; set; }
        public string RequiredString { get; set; }
        public int RequiredInt { get; set; }
        public decimal RequiredMoney { get; set; }
        public bool RequiredBool { get; set; }
        public DateTime RequiredDate { get; set; }
        public string NullableString { get; set; }
        public int? NullableInt { get; set; }
        public decimal? NullableMoney { get; set; }
        public bool? NullableBool { get; set; }
        public DateTime? NullableDate { get; set; }
    }

The testDatabase matches the class as far as nulls are concerned.

  static internal T NullConvertFromDB<T>(object value) 
        {
            if (value.Equals(DBNull.Value))
            {
                return default(T);
            }
            else
            {
                return (T)value;
            }
        }

        static internal object NullConvertToDB<T>(object value)
        {
            if (value == null)
                return System.DBNull.Value;
            else
                return (T)value;
        }

While retrieving data I use

 Test test = new Test();
                        test.PKId = dr.GetInt32(0);
                        test.RequiredString = dr.GetString(1);
                        test.RequiredInt = dr.GetInt32(2);
                        test.RequiredMoney = (decimal)dr.GetSqlMoney(3);
                        test.RequiredBool = dr.GetBoolean(4);
                        test.RequiredDate = dr.GetDateTime(5);
                        test.NullableString = NullConvertFromDB<string>(dr[6]);
                        test.NullableInt = NullConvertFromDB<int?>(dr[7]);
                        test.NullableMoney = NullConvertFromDB<decimal?>(dr[8]);
                        test.NullableBool = NullConvertFromDB<bool?>(dr[9]);
                        test.NullableDate = NullConvertFromDB<DateTime?>(dr[10]);

And inserting I use

 cmd.Parameters.AddWithValue("@RequiredString", test.RequiredString);
                    cmd.Parameters.AddWithValue("@RequiredInt", test.RequiredInt);
                    cmd.Parameters.AddWithValue("@RequiredMoney", test.RequiredMoney);
                    cmd.Parameters.AddWithValue("@RequiredBool", test.RequiredBool);
                    cmd.Parameters.AddWithValue("@RequiredDate", test.RequiredDate);
                    cmd.Parameters.AddWithValue("@NullableString", NullConvertToDB<string>(test.NullableString));
                    cmd.Parameters.AddWithValue("@NullableInt", NullConvertToDB<int?>(test.NullableInt));
                    cmd.Parameters.AddWithValue("@NullableMoney", NullConvertToDB<decimal?>(test.NullableMoney));
                    cmd.Parameters.AddWithValue("@NullableBool", NullConvertToDB<bool?>(test.NullableBool));
                    cmd.Parameters.AddWithValue("@NullableDate", NullConvertToDB<DateTime?>(test.NullableDate));

This works great but was wondering if there is anything I need to be aware of before I implement in in my DAL.

Regards

_Eric

  • 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-18T07:00:06+00:00Added an answer on May 18, 2026 at 7:00 am

    As a minor thing, is would probably be more efficient:

    if (value == null || value is DBNull) {... null code...}
    

    as a second thought, I don’t like the fact that if T=int you will get 0 for a null; I’d want an error. I’d be tempted to use:

    return (T)(object)null;
    

    which should work for reference types and Nullable<T> correctly, and raise an error for int etc.


    In ConvertToDb, the return (T)value; is redundant, since you are actually returning object; you may as well just:

    return value ?? DBNull.Value;
    

    (in both cases)


    So:

        static internal T NullConvertFromDB<T>(object value) 
        {
            if (value == null || value is DBNull)
            {
                return (T)(object)null;
            }
            return (T)value;
        }
    
        static internal object NullConvertToDB<T>(object value)
        {
            return value ?? DBNull.Value;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems to work fine in C#: class A : System.Attribute { public A()
This seems in my head like it should work but I cant figure out
This seems painfully simple, but I can't work out how to do it: I
Ok this seems so silly but I'm having some trouble getting this to work.
This evaluation in sql doesn't seems to work properly for some reason but i
This one seems to be a simple problem, but I can't make it work
I got this. But i can't seem to work out how to get the
This problem may seem like school work, but it isn't. At best it is
setTimeout(target.mousedown, 200) doesn't seem to work. I can do setTimeout(target.mousedown(), 200) , but this
I am trying to make this IF statement work, but I can't seem to

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.