2013-04-18 40 views
0

我知道這是對這裏的一些問題的重複,但相信我我已經嘗試了所有這些,並沒有運氣。多部分標識符「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(); 
     } 
+0

您沒有設置'參數Value'' @ ID',有你嗎? –

+0

listBox2.ValueMember =「id」;我想我已經設置它 – csharpvid

+0

不知道這可能是答案,但似乎你的listbox2.Item被錯誤地插入爲DataRowView。嘗試將'var table = new DataTable'更改爲'DataTable table = new DataTable()' – Steve

回答

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

然後調用

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

錯誤我編輯我的答案。 –

+0

無法將類型爲「System.Data.DataRowView」的對象轉換爲鍵入「System.Data.DataRow」。 – csharpvid

+0

@csharpvid對於我以前的帖子感到抱歉。我在你的代碼中修改了一些東西。 首先,從數據表創建一個方法來填充你的列表框。 私人無效PopulateListBox(數據表DT) { 的foreach(DataRow的行中dt.Rows) { ls.Items.Add(行[ 「ID」]); } } 然後,訪問該項目,使用ls.Items [0] 希望這會有所幫助! –

相關問題