Convert String to SqlDbType
10,004
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.
Author by
crjunk
Updated on June 30, 2022Comments
-
crjunk almost 2 years
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)
-
crjunk over 13 yearsYou 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)