473,698 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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["aColumnNam e"]; }
set{ m_DataRow["aColumnNam e"] = 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 GetColumnValueO rDefault(DataRo w dataRow, string columnName)
{
DataColumn column = dataRow.Table.C olumns[columnName];
if (column == null)
throw new Exception("Inva lid column name: " + columnName);

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

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

// Use reflection to create a new ValueType
System.Reflecti on.ConstructorI nfo constructor =
dataType.GetCon structor(System .Type.EmptyType s);
if (constructor != null) //// IS ALWAYS NULL!
{
object defaultValue = constructor.Inv oke(new object[0]);
return defaultValue;
}

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

if (dataType == typeof(System.B yte))
return m_Byte;

if (dataType == typeof(System.D ateTime))
return m_DateTime;

if (dataType == typeof(System.G uid))
return m_Guid;

if (dataType == typeof(System.I nt32))
return m_Int;

throw new Exception("Unab le to determine default for " + columnName);
}
Nov 16 '05 #1
0 2501

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

Similar topics

1
3284
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 i can see that the DefaultValue of the columns is system.dbnull!!!! But in SQL Server i have put as default values in a bigint field the '0' and
2
26689
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 empty elements in the XML. How to do that ? I don't understand why there is no simple option to specify the output format, or did I miss something ? regards andreas
4
10086
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 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.
11
4905
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 case if a column is NULL the it xhould return '1 and if not '2' How does DBNull.Value work?
2
1402
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 default)... Below is a snippet... <xs:element name="Customers"> <xs:complexType>
6
4707
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 get the exception:
9
2946
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 try to serialize/deserialize dataset schema, I get the error below during deserialization: "System.ArgumentException: The DefaultValue for column TestEnumField is of
11
1981
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 generated code. I put ***** around the problematic code. I've tried just removing this and it doesn't seem to work. There may be more generated code I don't know about. I could remove it but I am planning on doing a lot of refreshing of the data...
19
19332
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
34689
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
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.