I save a char value in a table, under the type “char(10)”.
When I try to get the values of a row in the table, I do the following:
DataTable table = MyAdoHelper.ExecuteDataTable(GlobalVar.dbName, Query);
The Ado helper is just a tool to extract the data from the table, nothing special about it.
When I do the next:
string MemberGender = (table.Rows[0]["member_gender"].ToString() == "m") ? "Male" : "Female";
But even when in the database there is a ‘m’, MemberGender is always “Female”.
How am I supposed to handle the char value?
Charvalues are padded with whitespace, so what you actually get is"m ". On the SQL Server, this behaviour can be turned on and off (for nullable chars) by using the SET ANSI_PADDING statement.To fix this on the C# side,
Trim()your value before comparing it:Alternatively, fix your database schema by changing the data type to
Char(1)orVarchar(10).