我知道這是對這裏的一些問題的重複,但相信我我已經嘗試了所有這些,並沒有運氣。多部分標識符「System.Data.DataRowView」無法綁定#2
我是新來的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」 無法綁定。
它來自於button1.Click
中的command.ExecuteNonQuery()
。
我試圖改變:
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();
}
您沒有設置'參數Value'' @ ID',有你嗎? –
listBox2.ValueMember =「id」;我想我已經設置它 – csharpvid
不知道這可能是答案,但似乎你的listbox2.Item被錯誤地插入爲DataRowView。嘗試將'var table = new DataTable'更改爲'DataTable table = new DataTable()' – Steve