简体   繁体   English

无法将“System.Byte”类型的 object 转换为“System.String”类型。

[英]Unable to cast object of type 'System.Byte' to type 'System.String'.'

I am trying to populate value from Database to Combobox and when I run it I get this error:我正在尝试将值从数据库填充到Combobox ,当我运行它时出现此错误:

Unable to cast object of type 'System.Byte' to type 'System.String'.'无法将“System.Byte”类型的 object 转换为“System.String”类型。

Status is of type tinyint . Statustinyint类型。

public void FillStatus()
{
    try
    {
        using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
        {
            conn.Open();
            string query = "SELECT DISTINCT Status FROM tblZaposleni_AD";
            SqlCommand cmd = new SqlCommand(query, conn);
            using (SqlDataReader saReader = cmd.ExecuteReader())
            {
                while (saReader.Read())
                {
                    string name = saReader.GetString(0);
                    ddlStatus.Items.Add(name);
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
}

I find where the error comes from.我找到了错误的来源。 Insted of代替于

string name = saReader.GetString(0);

I just replace this line to this one我只是将这一行替换为这一行

 string name = saReader.GetByte(0).ToString();

As SQL Server tinyint is converted to byte in .NET, you need to read it as such, then convert to a string:由于 SQL Server tinyint在 .NET 中被转为byte ,所以需要这样读取,然后转为字符串:

byte dbValue = saReader.GetByte(0);
string name = dbValue.ToString();
ddlStatus.Items.Add(name);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Byte' to type 'System.String' 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象。 发布后发生错误 - Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish (已解决)无法将“system.byte”类型的 object 转换为“system.string”类型(组合框填充 PictureBox) - (Solved) unable to cast object of type 'system.byte ' to type 'system.string' (combobox populate PictureBox) 无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象 - Unable to cast object of type 'System.Byte[]' to type 'System.String[]' Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。” - Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.” 无法将类型为“System.String”的 object 转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1 - Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1 从数据库读取数据导致无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的错误 - Reading data from database gives Unable to cast object of type 'System.Byte' to type 'System.String' error 无法将System.string强制转换为System.Byte [] - Unable to cast System.string to System.Byte[] 反序列化对象时,无法将类型为System.String的对象转换为类型为System.Byte [] - Cannot convert object of type System.String to type System.Byte[] when deserializing object 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM