5

I am trying to convert a string values into a SqlDbType. My code is able to covert "Text" into SqlDbType.Text without any errors but when I try to convert "bit" into SqlDbType.Bit, I receive the following error: "Requested value 'bit' was not found."

The same thing happens when trying to convert "int" into SqlDbType.Int Error Message: "Requested value 'int' was not found."

Why will this work for "text" but not "bit" or "int"?

Dim MyType as String = "bit"
Dim sdtype As SqlDbType

sdtype = DirectCast([Enum].Parse(GetType(SqlDbType), MyType), SqlDbType)

3 Answers 3

22

Because the enum value is SqlDbType.Bit, not SqlDbType.bit. Enum.Parse() is case-sensitive WRT to its input.

You need to use this overload of Enum.Parse:

SqlDbType type = (SqlDbType) Enum.Parse( typeof(SqlDbType) , "bit" , true ) ;

The third parameter indicates whether (true) or not (false) case should be ignored in parsing the enum's value from a string.

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

Comments

0

Looks like it's case-sensitive. Try "Bit" and see if that works out.

1 Comment

You are right. I just notice there is a flag that will ignore case. I added True and it works! sdtype = DirectCast([Enum].Parse(GetType(SqlDbType), MyType), SqlDbType, True)
0

What is the result of [Enum].Parse(GetType(SqlDbType))? Is this the result you expect? Because I think the error might be in there.

As a last alternative, could you try CTYpe instead if direct cast? (http://msdn.microsoft.com/en-us/library/7k6y2h6x(VS.71).aspx)

CType is slightly more flexible, however I do think it is a long shot, but maybe worth trying.

Comments

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.