InvalidCastException

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to cast DbType to an SqlDbType

(SqlDbType)DbType

This works fine for integers and strings but when I pass a DateTime I get an InvalidCastException

Is there any workaround about this

Thanks
 
I'm not sure I understand why exactly are you trying to cast one enum to
another?

Are you expecting to get, say, SqlDbType.DateTime(=4) out of
DbType.DateTime(=6)?
If so, it's not going to work. Casting DbType.DateTime(=6) to SqlDbType
will give you SqlDbType.Float(=6) instead.
This is not a very good match, isn't it?

If you trying to get a matching data type, you should use a look up table
or a case statement:


//
AnsiString = 0 Binary = 1 Byte = 2
SqlDbType[] DbTypeToSqlDbType = new SqlDbType[] { SqlDbType.VarChar,
SqlDbType.Binary, SqlDbType.TinyInt ... };

DbType dbt = DbType.Byte; //=2

SqlDbType sdb = DbTypeToSqlDbType[(int)dbt]; // = 20

If that's not what you trying to do, please provide more details.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Thread-Topic: InvalidCastException
thread-index: AcQnc0yP4+OIoOzGRwi6M6NgoEpIuw==
X-WN-Post: microsoft.public.dotnet.framework.compactframework
From: "=?Utf-8?B?QXJpcw==?=" <[email protected]>
Subject: InvalidCastException
Date: Wed, 21 Apr 2004 00:36:03 -0700
Lines: 9
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.compactframework
Path: cpmsftngxa10.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:51411
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I am trying to cast DbType to an SqlDbType:

(SqlDbType)DbType;

This works fine for integers and strings but when I pass a DateTime I get
an InvalidCastException.

Is there any workaround about this?

Thanks
 
What am I trying to do is to build an application that is database independent. That is my application uses IdbCommand, IdbDataReader etc... and in a switch statement I retrieve the name of the database from a configuration file.So when I pass a DbType in a IdbParameter, I cast it as an SqlDbType in a SqlParameter.
 
I see... Well, as I've explained in my previous post, you can not just cast
one enum to another.

You will get an enum with the same underlying numeric value, but the
meaning might be totally different.
In many cases there's no meaning at all.
For example, you can go ahead and cast System.Data.AcceptRejectRule to
System.IO.FileAccess, but it will accomplish nothing.

In this case there's a logical link between DbType and SqlDbType.
However, casting one to another won't work because underlying numeric
values are different for the same logical values.
It's up to you to connect them on a logical level. You have to look up
matching values using table or switch statement. Good luck.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Thread-Topic: InvalidCastException
thread-index: AcQoWRymUuqIP2aRQe2iG3XKXcwM3g==
X-WN-Post: microsoft.public.dotnet.framework.compactframework
From: "=?Utf-8?B?QXJpcw==?=" <[email protected]>
References: <[email protected]>
Subject: RE: InvalidCastException
Date: Thu, 22 Apr 2004 04:01:07 -0700
Lines: 1
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.compactframework
Path: cpmsftngxa10.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:51509
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

What am I trying to do is to build an application that is database
independent. That is my application uses IdbCommand, IdbDataReader etc...
and in a switch statement I retrieve the name of the database from a
configuration file.So when I pass a DbType in a IdbParameter, I cast it as
an SqlDbType in a SqlParameter.
 
Back
Top