将nvarchar值'System.Data.DataRowView'转换为数据类型int时转换失败 [英] Conversion failed when converting the nvarchar value 'System.Data.DataRowView' to data type int

查看:99
本文介绍了将nvarchar值'System.Data.DataRowView'转换为数据类型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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆