多部分标识符“System.Data.DataRowView”无法绑定#2

时间:2013-04-18 21:49:51

标签: c# .net

我知道这与一些问题重复,但请相信我,我已经尝试了所有这些问题,但没有运气。

我是C#+ MySQL的新手。

我有一个包含2个表,程序员和软件的数据库。我在软件表中有一个名为programmersid的列。这是一对多的关系。我有两个列表框。第一个是程序员的名字,当我点击程序员时,它会向我展示他所制作的软件。

当我点击软件时,文本将被放入文本框中,我有一个更新按钮。当我点击它时,它应该更新该软件的名称。

这是我的代码:

public partial class Form1 : Form
{
    private DataSet ds;
    private SqlConnection conn;
    private SqlDataAdapter programmersadapter;
    private SqlDataAdapter softwaresadapter;


    protected string connectionString = "Data Source=ICEBOX19-PC\\ICEBOX;Initial Catalog=software;Integrated Security=True";

    public Form1()
    {
        InitializeComponent();
        conn = new SqlConnection(connectionString);

        SqlCommand programmersselect = new SqlCommand("SELECT ID, Name FROM programmers", conn);
        programmersadapter = new SqlDataAdapter(programmersselect);

        SqlCommand softwaresselect = new SqlCommand("SELECT name FROM softwares WHERE programmerid = @ID", conn);
        softwaresselect.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
        softwaresadapter = new SqlDataAdapter(softwaresselect);
        showme();
    }
    private void showme()
    {
        ds = new DataSet();
        conn.Open();
        programmersadapter.Fill(ds, "programmers");
        conn.Close();
        listBox1.ValueMember = "id";
        listBox1.DisplayMember = "name";
        listBox1.DataSource = ds.Tables["programmers"];
    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        conn.Open();
        SqlCommand command = new SqlCommand("update softwares set name='" + textBox1.Text + "' where programmerid=" + listBox2.SelectedValue.ToString(), conn);
        command.ExecuteNonQuery();
        conn.Close();
        reload();
    }
    private void reload()
    {
        //listBox2.SelectedIndexChanged -= listBox2_SelectedIndexChanged;
        softwaresadapter.SelectCommand.Parameters["@id"].Value = listBox1.SelectedValue;
        var table = new DataTable();
        softwaresadapter.Fill(table);
        conn.Close();
        listBox2.DisplayMember = "name";
        listBox2.ValueMember = "id";
        listBox2.DataSource = table;
        //listBox2.SelectedIndexChanged += listBox2_SelectedIndexChanged;
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        reload();
    }

    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        textBox1.Text = listBox2.Text;
        textBox1.Text = textBox1.Text.Replace(" ", "");
    }
}

我得到了这个例外:

  

多部分标识符" System.Data.DataRowView"无法受约束。

它来自command.ExecuteNonQuery()中的button1.Click

我试图改变:

SqlCommand command = new SqlCommand("update softwares set name='" + textBox1.Text + "' where programmerid=" + listBox2.SelectedValue, conn);

SqlCommand command = new SqlCommand("update softwares set name='" + textBox1.Text + "' where programmerid=" + listBox2.SelectedValue.ToString(), conn);

但我也得到同样的东西。你们有什么想法吗?我现在正在寻找这个2小时。

PS:即使我尝试删除

,也会出现同样的错误
        private void delete_Click(object sender, EventArgs e)
        {
        conn.Open();
        SqlCommand command = new SqlCommand("delete from softwares where id="+listBox2.SelectedItem, conn);
        command.ExecuteNonQuery();
        conn.Close();
        }

1 个答案:

答案 0 :(得分:-1)

private void PopulateListBox(DataTable dt)
            {
                foreach (DataRow row in dt.Rows)
                {
                    ls.Items.Add(row["id"]);
                }
            }

然后,打电话

   string value = ls.Items[0].ToString();