Please see the case 1 and 2 below.
In case 2 : reader.GetString(reader.GetOrdinal("dmic_only")) line return "1" successfully
In case 1 : same code part in string.IsNullOrEmpty( reader.GetString(reader.GetOrdinal("dmic_only")) ) == false throws exception.
Exception : Unable to cast object of type 'System.Byte' to type 'System.String'.
dmic_only is a tinyint in database, not byte. It is really interesting. What is difference in cases?
Case 1:
if ((!reader.IsDBNull(reader.GetOrdinal("dmic_disallowed"))
&& string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_disallowed"))) == false)
&& (!reader.IsDBNull(reader.GetOrdinal("dmic_only"))
&& string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_only"))) == false))
{
retVal.Add("dmic_disallowed", reader.GetString(reader.GetOrdinal("dmic_disallowed")));
retVal.Add("dmic_only", reader.GetString(reader.GetOrdinal("dmic_only")));
}
Case 2 :
//if ((!reader.IsDBNull(reader.GetOrdinal("dmic_disallowed"))
//&& string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_disallowed"))) == false)
//&& (!reader.IsDBNull(reader.GetOrdinal("dmic_only"))
//&& string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_only"))) == false))
//{
retVal.Add("dmic_disallowed", reader.GetString(reader.GetOrdinal("dmic_disallowed")));
retVal.Add("dmic_only", reader.GetString(reader.GetOrdinal("dmic_only")));
//}
The exception you are getting is telling you exactly what you need to know...
reader.GetString(reader.GetOrdinal("dmic_only")) // will throw an error
as you have said dmic_only
is a tinyint
if you really want it as a string, you will have to unbox it first as a byte then to a string
string strValue = Convert.ToString((byte)reader["dmic_only"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.