473,419 Members | 4,382 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,419 software developers and data experts.

Infer SqlDbType Enumeration Member via Reflection

Hello all,

I'm in a situation where I need to retrieve a member from the
System.Data.SqlDbType 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.Reflection;
using System.Data;

namespace PleaseFreakinWork
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("System.Data.SqlDbType");
Console.WriteLine("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}

And when I run this, it blows up on the Console.WriteLine call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflection.Assembly, it works fine. But if I
change it to System.Data.SqlClient.SqlConnection (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 3449
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.WriteLine call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflection.Assembly, it works fine. But if I
change it to System.Data.SqlClient.SqlConnection (for example), that
doesn't work.


Yup. That's because System.Reflection.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 AssemblyQualifiedName property. For example,
for SqlConnection it's:

System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089

--
Jon Skeet - <sk***@pobox.com>
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.Varchar?

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

I'm in a situation where I need to retrieve a member from the
System.Data.SqlDbType 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.Reflection;
using System.Data;

namespace PleaseFreakinWork
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("System.Data.SqlDbType");
Console.WriteLine("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}

And when I run this, it blows up on the Console.WriteLine call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflection.Assembly, it works fine. But if I
change it to System.Data.SqlClient.SqlConnection (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.Varchar?

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

I'm in a situation where I need to retrieve a member from the
System.Data.SqlDbType 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.Reflection;
using System.Data;

namespace PleaseFreakinWork
{
public class TypeTest
{
public static void Main(string[] args)
{
try
{
Type MyType = Type.GetType("System.Data.SqlDbType");
Console.WriteLine("Type Name:" + MyType.Name);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}

And when I run this, it blows up on the Console.WriteLine call w/
"Object ref not set to an instance of an object". Now, if I change the
type name to System.Reflection.Assembly, it works fine. But if I
change it to System.Data.SqlClient.SqlConnection (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
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,...
11
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...
4
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...
1
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,...
2
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 ...
3
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...
7
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
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...
0
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...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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...
0
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...
0
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...
0
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...

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.