将nvarchar值'System.Data.DataRowView'转换为数据类型int时转换失败 [英] Conversion failed when converting the nvarchar value 'System.Data.DataRowView' to data type int
问题描述
我正在尝试基于comboBox选择填充DataGridView。我不知道我错在哪里plz帮助..
这是我的代码:
I am trying to populate DataGridView based on comboBox selection. I don know where i am going wrong plz help..
Here Is my Code:
public void GetTestGroups()
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter da = new SqlDataAdapter("select LabTestGroupId,GroupName from tbl_LabTestGroup", con);
DataTable dt = new DataTable();
da.Fill(dt);
cbTestGroup.DataSource = dt;
cbTestGroup.DisplayMember = "GroupName";
cbTestGroup.ValueMember = "LabTestGroupId";
}
}
private void cbTestGroup_SelectedIndexChanged(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter da = new SqlDataAdapter("spGetTestNames", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@LabTestGroupId", cbTestGroup.SelectedValue.ToString());
DataTable dt = new DataTable();
da.Fill(dt);// Error in this line.
dgvLabTests.DataSource = dt;
}
}
StoreProc:
StoreProc:
create procedure spGetTestNames
@LabTestGroupId int
as
begin
select TestNames from tbl_LabTests where LabTestGroupId = @LabTestGroupId
end
推荐答案
select TestNames from tbl_LabTests where LabTestGroupId = @LabTestGroupId
在这一行 @LabTestGroupId
是 int ,但是emingly LabTestGroupId
是 nvarchar ,其中一个值无法转换为int(因为它包含字符串System.Data.DataRowView!).. 。
检查你的表值并根据需要调整类型......
In this line @LabTestGroupId
is int, but seemingly LabTestGroupId
is nvarchar and one of it's value can not be converted to int (as it contains the string System.Data.DataRowView!)...
Check your table values and adjust the types as appropriate...
在你的代码中---
In your code ---
da.SelectCommand.Parameters.AddWithValue("@LabTestGroupId", cbTestGroup.SelectedValue.ToString());
参数类型为int。所以 da.SelectCommand.Parameters.AddWithValue(@ LabTestGroupId,cbTestGroup.SelectedValue);
或
Parameter type is int . so da.SelectCommand.Parameters.AddWithValue("@LabTestGroupId", cbTestGroup.SelectedValue);
or
da.SelectCommand.Parameters.AddWithValue("@LabTestGroupId", Convert.Toint(cbTestGroup.SelectedValue));
另一件事是
another thing is
da.Fill(dt);
cbTestGroup.DisplayMember = "GroupName";
cbTestGroup.ValueMember = "LabTestGroupId";
cbTestGroup.DataSource = dt;
这篇关于将nvarchar值'System.Data.DataRowView'转换为数据类型int时转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!