473,587 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Infer SqlDbType Enumeration Member via Reflection

Hello all,

I'm in a situation where I need to retrieve a member from the
System.Data.Sql DbType enumeration knowing only the type name.

At this point, I'm just trying to get reflection to work... This is
what I've got so far:

using System;
using System.Reflecti on;
using System.Data;

namespace PleaseFreakinWo rk
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("S ystem.Data.SqlD bType");
Console.WriteLi ne("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLi ne(ex.Message);
Console.WriteLi ne(ex.StackTrac e);
}
}
}
}

And when I run this, it blows up on the Console.WriteLi ne call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflecti on.Assembly, it works fine. But if I
change it to System.Data.Sql Client.SqlConne ction (for example), that
doesn't work.

I've tried loading the System.Data.dll assembly, but it errors out
saying that the file or one of its dependencies could not be found...

Help!

TIA,

Brett

Nov 17 '05 #1
3 3462
Brett Kelly <in*****@gmail. com> wrote:
At this point, I'm just trying to get reflection to work... This is
what I've got so far:
<snip>
And when I run this, it blows up on the Console.WriteLi ne call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflecti on.Assembly, it works fine. But if I
change it to System.Data.Sql Client.SqlConne ction (for example), that
doesn't work.


Yup. That's because System.Reflecti on.Assembly is in mscorlib, but
SqlConnection isn't. If you don't provide an assembly name in the type
name, only mscorlib and the currently executing assembly are searched.

To find what to use for a particular type, use typeof(...) in a test
program and write out the AssemblyQualifi edName property. For example,
for SqlConnection it's:

System.Data.Sql Client.SqlConne ction, System.Data, Version=1.0.500 0.0,
Culture=neutral , PublicKeyToken= b77a5c561934e08 9

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
I could be off base here; but am I reading correctly that you want to be
able to retrieve the enumeration value based on the name of the member, ie
given the string "VarChar", you want to set a value to SqlDbType.Varch ar?

If this is the case; SqlDbType value =
(SqlDbType)Enum .Parse(typeof(S qlDbType), "VarChar", true); will achieve it.
If not, please ignore me.
"Brett Kelly" <in*****@gmail. com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hello all,

I'm in a situation where I need to retrieve a member from the
System.Data.Sql DbType enumeration knowing only the type name.

At this point, I'm just trying to get reflection to work... This is
what I've got so far:

using System;
using System.Reflecti on;
using System.Data;

namespace PleaseFreakinWo rk
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("S ystem.Data.SqlD bType");
Console.WriteLi ne("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLi ne(ex.Message);
Console.WriteLi ne(ex.StackTrac e);
}
}
}
}

And when I run this, it blows up on the Console.WriteLi ne call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflecti on.Assembly, it works fine. But if I
change it to System.Data.Sql Client.SqlConne ction (for example), that
doesn't work.

I've tried loading the System.Data.dll assembly, but it errors out
saying that the file or one of its dependencies could not be found...

Help!

TIA,

Brett

Nov 17 '05 #3
Sir, that is *precisely* what I was after :)

Thanks!

Martin Robins wrote:
I could be off base here; but am I reading correctly that you want to be
able to retrieve the enumeration value based on the name of the member, ie
given the string "VarChar", you want to set a value to SqlDbType.Varch ar?

If this is the case; SqlDbType value =
(SqlDbType)Enum .Parse(typeof(S qlDbType), "VarChar", true); will achieve it.
If not, please ignore me.
"Brett Kelly" <in*****@gmail. com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hello all,

I'm in a situation where I need to retrieve a member from the
System.Data.Sql DbType enumeration knowing only the type name.

At this point, I'm just trying to get reflection to work... This is
what I've got so far:

using System;
using System.Reflecti on;
using System.Data;

namespace PleaseFreakinWo rk
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("S ystem.Data.SqlD bType");
Console.WriteLi ne("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLi ne(ex.Message);
Console.WriteLi ne(ex.StackTrac e);
}
}
}
}

And when I run this, it blows up on the Console.WriteLi ne call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflecti on.Assembly, it works fine. But if I
change it to System.Data.Sql Client.SqlConne ction (for example), that
doesn't work.

I've tried loading the System.Data.dll assembly, but it errors out
saying that the file or one of its dependencies could not be found...

Help!

TIA,

Brett


Nov 17 '05 #4

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

Similar topics

1
408
by: Greg | last post by:
Hi I am retrieving some records from SQLCE database and storing them in DataSet I am trying to get database columns DataTypes Using my_dataset.Mytable.My_column.DataType gives me System.Type, not SqlDbType How I could get SqlDbType…. Any suggestions would be really appreciated
11
4222
by: Vinod I | last post by:
Hi Team, I am having a string as "System.Data.SqlDbType.Int". Now I want to convert this string type to actual type to use with my Command object Parameter Creation. How I will convert this string to the type object ?. Thanks in advance
4
8353
by: Alicia | last post by:
Hi all, I have a problem with an Enum and Reflection. I am using an Xml and Reflection to create some controls, and to set their properties. All goes well until I encounter one property which is defined like this: public enum MyType { Alpha = 0, Beta = 1 }
1
3661
by: studen77 | last post by:
Thanks to anyone in advance who can help :) Ok, I know this is a far stretch, but what I'm trying to do is to cast a System.object as one of the SqlDbType enumerations (SqlDbType.Int, -.NVarchar, etc.) Is this anyway possible? Through inheritance, or anything?
2
1807
by: Mark | last post by:
Assume you have an enumeration like PhoneType { Home, Business, Cell }. This enumeration corresponds with a lookup/dictionary table in your database like: phone_cd | phone_descr 1 Home 2 Business 3 Cell Now, I could associated each enumeration value with the phone_cd used in the
3
10618
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am after. Create a class named “clsMyItems” and in that class place an enum. Public Enum Items Item1 = 0 Item2 = 1 Item3 = 2
7
9008
by: Don | last post by:
I need to get the type of an enumeration from an instance of a class. e.g. Public Class MyClass Public Enum MyEnum value1 = 1 End Enum End Class
0
1770
by: bborowin | last post by:
Hi there, Given a collection of SqlDbType enums (eg, {BigInt, VarChar, Bit}), I need to convert it to a collection of corresponding C# Types (here, {long, string, bool}). The contents of the source collection is not known ahead of time; it could be any valid combination of enums in the SqlDbType enumeration. Short of writing an explicit switch statement for all elements in the
0
1822
by: rbrowning1958 | last post by:
Hello, Trying to compile some code I was given. Vs 2005 patches applied AFAICT. Help / About reports visual studio versiopn 8.0.50727.867 and framework version 2.0.50727. Code references SqlDbType.date and receives compilation error that the enumeration does not contain a definition for Date. I looked at the definition and sure enough my enumeration ends with:
0
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8349
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
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5722
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
5395
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
3845
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
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
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
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.