473,395 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to get ValueType default for DBNull column using reflection?

I have properties that wrap DataRow columns as in:
public int aNumber
{ get{ return m_DataRow["aColumnName"]; }
set{ m_DataRow["aColumnName"] = value; } }

If the column happens to contain DBNull, I get a cast
exception since DBNull cannot be converted to int. I
wrote the following method that looks up the column's
data type and if it is a ValueType, returns the default
value for the ValueType.

I want the method to use reflection to get the default
value, so I don't have to code for each data type. However,
GetConstructor always returns null. Is there some other
way to get the default value for a ValueType?

Thanks,

Brian Brane

Here's the method...

// Instance variables containing default values (work around)
bool m_Bool;
byte m_Byte;
DateTime m_DateTime;
Guid m_Guid;
int m_Int;

private object GetColumnValueOrDefault(DataRow dataRow, string columnName)
{
DataColumn column = dataRow.Table.Columns[columnName];
if (column == null)
throw new Exception("Invalid column name: " + columnName);

// If the column contains a value, then return the value
object value = dataRow[column];
if (value != System.DBNull.Value)
return value;

// If the column is not a ValueType, then return null
System.Type dataType = column.DataType;
if (!dataType.IsValueType)
return null;

// Use reflection to create a new ValueType
System.Reflection.ConstructorInfo constructor =
dataType.GetConstructor(System.Type.EmptyTypes);
if (constructor != null) //// IS ALWAYS NULL!
{
object defaultValue = constructor.Invoke(new object[0]);
return defaultValue;
}

// Return a ValueType that is initialized to its default value
if (dataType == typeof(System.Boolean))
return m_Bool;

if (dataType == typeof(System.Byte))
return m_Byte;

if (dataType == typeof(System.DateTime))
return m_DateTime;

if (dataType == typeof(System.Guid))
return m_Guid;

if (dataType == typeof(System.Int32))
return m_Int;

throw new Exception("Unable to determine default for " + columnName);
}
Nov 20 '05 #1
2 3819
Cor
Hi Brian,

I think you are posting to the wrong newsgroup, I think you did want to be
2 rows higher (if you see the JavaScritp group above that is CSharp)

:-)

Cor

"Brian Brane" <br*********@msn.com> schreef in bericht
news:24**************************@posting.google.c om...
I have properties that wrap DataRow columns as in:
public int aNumber
{ get{ return m_DataRow["aColumnName"]; }
set{ m_DataRow["aColumnName"] = value; } }

If the column happens to contain DBNull, I get a cast
exception since DBNull cannot be converted to int. I
wrote the following method that looks up the column's
data type and if it is a ValueType, returns the default
value for the ValueType.

I want the method to use reflection to get the default
value, so I don't have to code for each data type. However,
GetConstructor always returns null. Is there some other
way to get the default value for a ValueType?

Thanks,

Brian Brane

Here's the method...

// Instance variables containing default values (work around)
bool m_Bool;
byte m_Byte;
DateTime m_DateTime;
Guid m_Guid;
int m_Int;

private object GetColumnValueOrDefault(DataRow dataRow, string columnName)
{
DataColumn column = dataRow.Table.Columns[columnName];
if (column == null)
throw new Exception("Invalid column name: " + columnName);

// If the column contains a value, then return the value
object value = dataRow[column];
if (value != System.DBNull.Value)
return value;

// If the column is not a ValueType, then return null
System.Type dataType = column.DataType;
if (!dataType.IsValueType)
return null;

// Use reflection to create a new ValueType
System.Reflection.ConstructorInfo constructor =
dataType.GetConstructor(System.Type.EmptyTypes);
if (constructor != null) //// IS ALWAYS NULL!
{
object defaultValue = constructor.Invoke(new object[0]);
return defaultValue;
}

// Return a ValueType that is initialized to its default value
if (dataType == typeof(System.Boolean))
return m_Bool;

if (dataType == typeof(System.Byte))
return m_Byte;

if (dataType == typeof(System.DateTime))
return m_DateTime;

if (dataType == typeof(System.Guid))
return m_Guid;

if (dataType == typeof(System.Int32))
return m_Int;

throw new Exception("Unable to determine default for " + columnName);
}

Nov 20 '05 #2
Will do. Thanks!
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Programmer | last post by:
Hi All Here is my problem I'm using a SQLDataAdapter and DataSet I use the method FillSchema(myDataset, SchemaType.Source) The problem is that when i Check the default Values of the Dataset...
2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
11
by: Patrick.O.Ige | last post by:
When i try and use this (Where Unit is a column in my Table):- If Unit Is DBNull.Value Then Return "1" Else Return "2" End If I always have 2 returned! Even when Unit is NULL! I want a...
2
by: mjwills | last post by:
All, I am using VB.NET 1.0 / 2002. I am having trouble setting a column default to "" (note that "" is not the same as Null / DBNull / Nothing) in a typed dataset (to reflect the same database...
6
by: vvenk | last post by:
Hello: When I try to do the following, Dim liTemp As System.DBNull If c.Value Is Nothing Then newDataTableRow(c.Column.Key) = liTemp Else newDataTableRow(c.Column.Key) = c.Value End If I...
9
by: PeterWellington | last post by:
I have a column in a data table that stores enum values and assigns a default value: Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek)) dc.DefaultValue = DayOfWeek.Thursday When I...
11
by: Greg P | last post by:
I'm using VB 2005, and have drug and dropped datagridviews onto my form. The default code that is generated for inserts tries to insert a value for the auto generated primary key. Below is the...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
9
by: Robert Bravery | last post by:
HI all, I have a column value returned to a string variable in my c# app. But the return type is of system.dbnull. How can I convert that to a system.sting Thanks Robert
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.