简体   繁体   English

C# 无法将“system.string”类型的对象转换为“system.int32”类型

[英]C# unable to cast object of type 'system.string' to type 'system.int32'

I am getting the error unable to cast object of type 'system.string' to type 'system.int32' when retrieving a varchar column that contains data and trying it to display it in a ComboBox.检索包含数据的varchar列并尝试将其显示在 ComboBox 中时,出现unable to cast object of type 'system.string' to type 'system.int32'的错误。

Here is my code:这是我的代码:

private void GetFinancialCatDeactivate(int active)
        {
            try
            {
                using (var Connect = new SqlConnection(connstr))
                {
                    Connect.Open();
                    using (var Command = new SqlCommand("[dbo].[spParametresFinancesTb_GetCategories]", Connect))
                    {
                        Command.CommandType = CommandType.StoredProcedure;
                        Command.Parameters.Add("@active", SqlDbType.VarChar).Value = active;
                        SqlDataReader dr = Command.ExecuteReader();
                        while (dr.Read())
                        {
                            string catlist = dr.GetString(0)

                            ComboBoxFinanceNameDeactivate.Items.Add(catlist);
                            ComboBoxFinanceNameDeactivate.Text = catlist;
                            ComboBoxFinanceDefaultNameEdit.Items.Add(catlist);
                            ComboBoxFinanceDefaultNameEdit.Text = catlist;                            

                        }
                        dr.Close();
                    }

                    Connect.Close();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

The code is executing a stored procedure which is here:代码正在执行一个存储过程,它在这里:

ALTER PROCEDURE [dbo].[spParametresFinancesTb_GetCategories]
(
    @active numeric(1)
 )
AS
BEGIN
    BEGIN TRY
        SET NOCOUNT ON
            BEGIN
                    select category
                    from dbo.PARAMETRES_FINANCES 
                    where active = @active;
                END;
    END TRY
    BEGIN CATCH     
        SELECT
        ERROR_PROCEDURE () as ProcedureErreur,
        ERROR_MESSAGE () as MessageErreur,
        ERROR_LINE () as LigneErreur,
        ERROR_SEVERITY () as SeveriteErreur
    END CATCH
END;

The error is appearing here: ComboBoxFinanceDefaultNameEdit.Text = catlist;错误出现在这里: ComboBoxFinanceDefaultNameEdit.Text = catlist; in the code-behind.在代码隐藏中。

I find it very weird since the fist combobox is not displaying the error.我觉得很奇怪,因为第一个组合框没有显示错误。

Here is how both comboboxes are made (XAML):以下是两个组合框的制作方式 (XAML):

<ComboBox Name="ComboBoxFinanceDefaultNameEdit" Style="{StaticResource ComboBoxStyle}" SelectionChanged="ComboBoxFinanceDefaultNameEdit_SelectionChanged"/>
<ComboBox Name="ComboBoxFinanceNameDeactivate" Grid.Column="1" Style="{StaticResource ComboBoxStyle}"/>

I tried changing the ComboBoxFinanceDefaultNameEdit.Text = catlist;我尝试更改ComboBoxFinanceDefaultNameEdit.Text = catlist; to ComboBoxFinanceDefaultNameEdit.SelectedItem= catlist;ComboBoxFinanceDefaultNameEdit.SelectedItem= catlist; but it dind't change a thing.但它并没有改变任何事情。

Any idea what is going on here?知道这里发生了什么吗?

I am on .Net Core 3.1.101我在 .Net Core 3.1.101

EDIT编辑

I have updated the question to add more information about the problem.我已更新问题以添加有关该问题的更多信息。

EDIT 2编辑 2

I have noticed that the problem is appearing mainly on comboboxes where I have implemented a selectionchanged event.我注意到问题主要出现在我实现了 selectionchanged 事件的组合框上。 Without this event, no error.没有这个事件,就没有错误。 Still investigating how to fix this.仍在研究如何解决此问题。

EDIT 3编辑 3

I would like to mention that the error is shown as a messagebox.我想提一下错误显示为消息框。 When I debug, it doesn't throw an exception but rather a messagebox.当我调试时,它不会抛出异常,而是一个消息框。 However, the data is still displayed in the combobox.但是,数据仍显示在组合框中。 The combobox where this problem appears is the one where I have a selectionchange event出现此问题的组合框是我有 selectionchange 事件的组合框

I think the problem is with the @active parameter.我认为问题出在@active 参数上。 You see you have defined it as SqlDbType.VarChar你看你已经把它定义为SqlDbType.VarChar

 Command.Parameters.Add("@active", SqlDbType.VarChar).Value = active;

But in your stored procedure you have marked it as @active numeric(1) .但是在您的存储过程中,您已将其标记为@active numeric(1)

暂无
暂无

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

相关问题 错误:无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - Error: Unable to cast object of type 'System.Int32' to type 'System.String' 在LINQ查询中无法将类型为&#39;System.Int32&#39;的对象转换为类型为&#39;System.String&#39;的对象 - Unable to cast object of type 'System.Int32' to type 'System.String' in LINQ query Contoso 大学项目:InvalidCastException:无法将“System.String”类型的对象转换为“System.Int32”类型 - Contoso University Project: InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32' 使用 ToListAsync() 时无法将类型为“System.String”的 object 转换为类型“System.Int32” - Unable to cast object of type 'System.String' to type 'System.Int32' while using ToListAsync() 附加信息:无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - Additional information: Unable to cast object of type 'System.Int32' to type 'System.String' 无法转换类型为system.int32的对象以从数据库中键入system.string组合框填充 - unable to cast object of type system.int32 to type system.string combobox fill from database 无法在 DataReader.GetString() 中将“System.Int32”类型的对象转换为“System.String”类型 - Unable to cast object of type 'System.Int32' to type 'System.String' in DataReader.GetString() InvalidCastException:无法将“System.Int32”类型的对象转换为“System.String”类型 - InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String' 错误无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - Error Unable to cast object of type 'System.Int32' to type 'System.String' InvalidCastException:无法将“System.String”类型的对象转换为“System.Int32”类型 - InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM