Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Handling Null Value In DataSet
Message
De
28/10/2009 21:34:16
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01432130
Message ID:
01432146
Vues:
52
Hey Kevin,

I've posted this before:

I have a CommonFunctions class that I use for things such as a GetNonNull( ) method (it's an old class, dating back to the 1.1 days, but it still works fine and so I have never updated it for 2.0, haven't really looked to see if it's even necessary). 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






>I'm doing this:
>
>
>_RoleKey = (int)dsDataSet.Tables[0].Rows[0]["RoleKey"];
>
>
>It's throwing an exception because RoleKey is null in the data. How do I handle this?
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform