Error while executing CHAR (1) SQL column as "char" in code

I choose from a column CHAR(1)

of SQL Server 2000 with the name of the column Combo_Label

- it will always contain one character A

.. Z

. During testing, it converts the first 70 or so items without issue, but then runs into Invalid Cast Exception

.

This is problem:

char comboLabel = (char)formCombo.Rows[j]["Combo_Label"];

      

This is a screenshot of the watch list showing some ways to evaluate it.

http://i.imgur.com/iaEBEDZ.png

Any thoughts on why this is happening?

0


source to share


3 answers


There is no concept in database and Db access API char

. Yours is CHAR(1)

displayed on string

.

probably the smartest option:

  string comboLabel = (string)formCombo.Rows[j]["Combo_Label"];

      



but if you really want char

:

  char comboLabel = ((string)formCombo.Rows[j]["Combo_Label"])[0];

      

+2


source


The field is a string, you can do something like this, but also needs to be considered if the hte field is null or the emmpty string



char comboLabel = ((string)formCombo.Rows[j]["Combo_Label"])[0];

0


source


first convert this to string then get the first character ...

char comboLabel = formCombo.Rows[j]["Combo_Label"].ToString()[0];

      

0


source







All Articles