Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SmallInt Cast
Message
From
12/11/2007 14:29:09
 
 
To
12/11/2007 14:22:56
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Title:
Miscellaneous
Thread ID:
01268522
Message ID:
01268555
Views:
10
You're welcome, Stuart. Kevin probably has some built-in methods for handling this kind of stuff, but I don't use MM, so I don't know if he does.

I have a CommonFunctions class that I use for things such as a GetNonNull( ) method. This is implemented with many overloads, but here's an example of just a few:
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:
string MyString = CommonFunctions.GetNonNull(MyDataSet.Tables[0].Rows[0]["MyColumn"], "");
// -or-
DateTime MyDatetime = CommonFunctions.GetNonNullDate(MyDataSet.Tables[0].Rows[0]["MyDateColumn"])
~~Bonnie




>hi
>I had a little test that gets one row from the table then adds that record back to the table, had not thought to check what value was in the table its null and thats was why it was failing Doa!
>thanks for that
>Stuart
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform