[英]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.