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

查看:188
本文介绍了将varchar值'System.Data.DataRowView'转换为数据类型int时转换失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject成员,



我试图根据用户在组合框中做出的选择,在C#窗体中填充sql中的数据,尽管我已成功将组合框绑定到数据库,但我无法根据组合框中项目的选择填充文本框中的数据。

当我运行程序时,我得到的错误是--->

SqlException未被用户代码处理 - 将varchar值'System.Data.DataRowView'转换为数据类型int时转换失败。

我的代码如下





//数据库表



1 )id-int-notnull(主键和自动生成id)

2)name-varchar-notnull

3)mobile-numeric-notnull

4)address-varchar-notnull

Hello Codeproject Member ,

I am trying to populate data from sql in a C# windows form according to the selection made by the user in a combobox , though I have managed to successfully bind the combobox to the database , but I am not able to populate the data in the textboxes according to the selection of an item from combobox.
When I run program the error I am getting is--->
SqlException was unhandled by user code -"Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int."
My code is as following


//Database Table

1) id-int-notnull (primary key and autogenerate id)
2) name-varchar-notnull
3) mobile-numeric-notnull
4) address-varchar-notnull

private void Form2_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Database path.............");
        conn.Open();
        SqlCommand command = new SqlCommand("select * from customer", conn);
        SqlDataAdapter adp = new SqlDataAdapter(command);
        tbl = new DataTable();
        adp.Fill(tbl);
        comboBox1.DataSource = tbl;
     comboBox1.ValueMember = tbl.Columns[0].ColumnName;
 }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Database path...............");
        conn.Open();
       SqlCommand command = new SqlCommand("select name,mobile,address from customer where id=@id", conn);
    command.Parameters.AddWithValue("@id", comboBox1.SelectedValue.Tostring());
        SqlDataAdapter adp = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        adp.Fill(ds, "customer");
        textBox1.Text = ds.Tables["customer"].Rows[1]["name"].ToString();
     textBox2.Text = ds.Tables["customer"].Rows[2]["mobile"].ToString();
        textBox3.Text = ds.Tables["customer"].Rows[3]["address"].ToString();
       }

推荐答案

问题正在发生,因为参数值未设置为您可能的值期待。表中的id列是一个int数据类型,这是您传递它所需要的。请参阅下面的代码:



The problem is occurring because the parameter value is not getting set to what you are probably expecting. Your "id" column in the table is an int datatype and that is what you need to pass it. See code below:

int comboBoxChoice = Convert.ToInt32(comboBox1.SelectedValue.ToString());
command.Parameters.AddWithValue("@id", comboBoxChoice);





现在提供你的组合框获得一个值,这应该可以解决问题。



Now provided your combo box is getting a value, this should do the trick.


ComboBox.Selected< Something> 属性存在已知问题。看看这里: ComboBox SelectedItem,SelectedValue,SelectedWhat? ?? [ ^ ]

There are known issues with ComboBox.Selected<Something> properties. Have a look here: ComboBox SelectedItem, SelectedValue, SelectedWhat???[^]
Quote:

SelectedValue - 此属性取决于值ValueMember。



如果属性ValueMember不是Nothing,ComboBox将在SelectedItem上查找具有ValueMember中指定名称的成员并返回该成员。这也是ComboBox中显示的值。

SelectedValue - This property depends on the value of ValueMember.

If the property ValueMember is not Nothing the ComboBox will look for a member on SelectedItem with the name specified in ValueMember and return that. This is also the value displayed in the ComboBox.





我建议你使用 Int32.TryParse [ ^ ]方法或更好地处理 SelectedItem SelectedIndex property。



I'd suggest you to use Int32.TryParse[^] method or better to deal with SelectedItem or SelectedIndex property.


这篇关于将varchar值'System.Data.DataRowView'转换为数据类型int时转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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