0
Dim MyType as String = "numeric"
Dim sdtype As SqlDbType  sdtype = DirectCast([Enum].Parse(GetType(SqlDbType), MyType), SqlDbType) 

Not working for numeric. I want to know how to handle this data types. Thanks in advance.

1 Answer 1

1

The SqlDbType enumeration simply does not contain the value "numeric" so it's not possible to cast such string to that enum.

To avoid error, use such code: (C#, not familiar with VB.NET sorry)

SqlDbType dbType;
if (Enum.TryParse<SqlDbType>(myType, true, out dbType))
{
    MessageBox.Show("type: " + dbType);
}
else
{
    MessageBox.Show("invalid type: " + myType);
}

There are various numeric types available though, e.g. Float so changing the string to one of those should work just fine:

Dim MyType as String = "float"

All available data types for SQL Server database provider are:

BigInt
Binary
Bit
Char
DateTime
Decimal
Float
Image
Int
Money
NChar
NText
NVarChar
Real
UniqueIdentifier
SmallDateTime
SmallInt
SmallMoney
Text
Timestamp
TinyInt
VarBinary
VarChar
Variant
Xml
Udt
Structured
Date
Time
DateTime2
DateTimeOffset

Any attempt to cast string not in this list will result in error.

Edit: on second thought looks like you are working with Access database? In such case, use OleDbType and not SqlDbType, just change the code to:

Dim sdtype As OleDbType  sdtype = DirectCast([Enum].Parse(GetType(OleDbType), MyType), OleDbType)

And it should work, since OleDbType does contain "Numeric".

Sign up to request clarification or add additional context in comments.

2 Comments

can you plaese tell me what are the other data types whose defination is not present in SqlDbType enum.
Not really, I can only tell what is present. Anyway, see my edit I think I figured the source of your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.