Reputation: 969
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)
Upvotes: 5
Views: 11471
Reputation: 74267
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.
Upvotes: 22
Reputation: 9638
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.
Upvotes: 0
Reputation: 40393
Looks like it's case-sensitive. Try "Bit" and see if that works out.
Upvotes: 0