首页 > 解决方案 > “指定的演员阵容无效。” 同时填充 ComboBoxe

问题描述

我正在尝试在按钮单击中填充 ComboBox,但是我收到以下错误:

System.InvalidCastException:“指定的演员表无效。”

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

我试图转换string name为,int但这不起作用。我不知道错误来自哪里。

顺便说一句:OrgUnitId在数据库中是类型bigint

标签: c#sql-server

解决方案


C# 中的 BigInt 等效项是 Int64。

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

参考 - https://stackoverflow.com/a/968734/1182982


推荐阅读