C# 值组合框更改C中文本框的值

C# 值组合框更改C中文本框的值,c#,winforms,combobox,C#,Winforms,Combobox,各位专家您好,我不久前使用过这段代码,它是有效的,但现在不起作用了。。。!!上面说 转换varchar值时转换失败 将“System.Data.DataRowView”更改为数据类型int 代码是combobox select value change文本框,其中包含此值的详细信息,并提前向您表示感谢 public partial class test : Form { SqlConnection cn = new SqlConnection("Server=AMEER;DataBa

各位专家您好,我不久前使用过这段代码,它是有效的,但现在不起作用了。。。!!上面说

转换varchar值时转换失败 将“System.Data.DataRowView”更改为数据类型int

代码是combobox select value change文本框,其中包含此值的详细信息,并提前向您表示感谢

 public partial class test : Form
{

    SqlConnection cn = new SqlConnection("Server=AMEER;DataBase=custemer_net;Integrated security=true");
    SqlDataAdapter da;
    DataTable dt = new DataTable();
    DataTable dttt = new DataTable();
    public test()
    {
        InitializeComponent();
        da = new SqlDataAdapter("Select *from subscrbtion ", cn);
        da.Fill(dt);
        comboBox1.DisplayMember = "name";
        comboBox1.ValueMember = "id";
        comboBox1.DataSource = dt;

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("select * from subscrbtion where id='" + comboBox1.SelectedValue + "'", cn);
        da.Fill(dttt);
        textBox1.Text = dttt.Rows[0]["phone"].ToString();
    }

我认为您的SqlDataAdapter中有一个错误:

new SqlDataAdapter("select * from subscrbtion where id='" + comboBox1.Text + "'", cn);
应该是

new SqlDataAdapter("select * from subscrbtion where id='" + comboBox1.SelectedValue + "'", cn);
SelectedValue将指向您在ValueMember下声明的Id

额外:在声明其数据/值成员后设置combobox数据源:

comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
comboBox1.DataSource = dt;
请根据以下内容更改代码:

public test()
{
    InitializeComponent();

    using(SqlConnection cn = new SqlConnection("Server=AMEER;DataBase=custemer_net;Integrated security=true"))
    {
        var adapter = new SqlDataAdapter("Select *from subscrbtion ", cn);
        adapter.Fill(dt);
    }
    comboBox1.DisplayMember = "name";
    comboBox1.ValueMember = "id";
    comboBox1.DataSource = dt;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //for testing
    MessageBox.Show(comboBox1.SelectedValue.ToString());
    //

    //clear the datatable dttt
    dttt.Clear();                  //<----------THIS SHOULD BE YOUR NEW SOLUTION

    //refill the datatable with the new information
    using(SqlConnection cn = new SqlConnection("Server=AMEER;DataBase=custemer_net;Integrated security=true"))
    {
        var adapter = new SqlDataAdapter("select * from subscrbtion where id='" + comboBox1.SelectedValue + "'", cn);
        adapter.Fill(dttt);
    }
    //for testing
    MessageBox.Show(dttt.Rows.Count.ToString());
    //

    //show the data inside the textbox.
    textBox1.Text = dttt.Rows[0]["phone"].ToString();
}
让我知道消息框掉了什么

编辑: 根据msdn:SqlDataAdapter.Fill方法在数据库中添加值。==>这意味着为什么测试消息框总是随着行的增加而增加


我在上面的代码中添加了值,希望这有帮助

您在这里遇到了什么错误/异常?谢谢亲爱的我,但是错误现在很抱歉出错了错误何时消失?我假设textBox1.Text=dttt.Rows[0][phone].ToString;另外,请在声明数据和值memberi do后设置combobox数据源,但现在将varchar值“aa”转换为数据类型int时错误是转换失败。aa是databaseAt中的值,而代码行中出现异常?我认为您给我们分享的代码部分是错误的,因为异常包含转换为int的错误,这在您的代码中不存在。谢谢,它现在可以正常工作,没有错误,但textbox的值不会随选择而更改,并在textbox中显示第一个值,但不会更改change@ameer; 您是否可以使用已实施的更改编辑您的问题?通过这种方式,我们可以提供进一步的帮助,不清楚你做了哪些改变,哪些没有。我编辑我的朋友这个问题让我很生气,因为我总是使用这种方法抱歉,我不明白。是的,它是winforms应用程序,你的意思是你太棒了,我该怎么感谢你呢?专家,谢谢你,亲爱的朋友,因为你,我开始爱比利时了^