public class CommonFunctions { public static object GetNonNull(object Test, object Default) { if (Test != DBNull.Value && Test != null) return Test; else return Default; } public static string GetNonNull(object Test, string Default) { if (Test != DBNull.Value && Test != null) { if(Test is DateTime) { DateTime TestDT = Convert.ToDateTime(Test); DateTime SqlNull = new DateTime(1900, 1, 1); if(TestDT == SqlNull) return Default; } else if (Test is bool) { bool YesNo = Convert.ToBoolean(Test); if (YesNo) return "Yes"; else return "No"; } return Test.ToString().Trim(); } else return Default; } public static int GetNonNull(object Test, int Default) { if (Test != DBNull.Value && Test != null) return Convert.ToInt32(Test); else return Default; } public static DateTime GetNonNull(object Test, DateTime Default) { if (Test != DBNull.Value && Test != null) { DateTime TestDT = Convert.ToDateTime(Test); DateTime SqlNull = new DateTime(1900, 1, 1); DateTime NetNull = new DateTime(1, 1, 1); if(TestDT != SqlNull && TestDT != NetNull) return TestDT; else return Default; } else return Default; } }These are just a sample of the overloads I use. I have a few more such as for bool, decimal, long and even a few differently named methods specifically for Dates, such as these next two;
public static string GetNonNullDate(object Test, string Default) { if (Test != DBNull.Value && Test != null) { if(Test is DateTime) { DateTime TestDT = Convert.ToDateTime(Test); DateTime SqlNull = new DateTime(1900, 1, 1); if(TestDT != SqlNull) return TestDT.ToShortDateString(); } return Default; } else return Default; } public static DateTime GetNonNullDate(object Test) { if (Test != DBNull.Value && Test != null && Test is DateTime) return Convert.ToDateTime(Test); else return new DateTime(1900, 1, 1); }Anyway, you get the point. Note that these are static methods, so no instantiation of the CommonFunctions class is necessary.To use them, you would simply have something like this:
>_RoleKey = (int)dsDataSet.Tables[0].Rows[0]["RoleKey"]; >>