Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to SqlDbType

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)
like image 995
crjunk Avatar asked Dec 02 '22 03:12

crjunk


1 Answers

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.

like image 94
Nicholas Carey Avatar answered Dec 04 '22 00:12

Nicholas Carey