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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With