2016-09-28 45 views
-1

當前一個我做的,從MsDatabase和字符串後選擇數據後沒有按下刪除按鈕時,來自一個或多個參數給定值

query = "delete from RegisterItem where Name =" +txtName1.Text+""; 

它顯示了一個錯誤說沒有從一個或多個給定值參數,雖然我已經檢查txtName1匹配名稱

{ 
    OleDbConnection connect = new OleDbConnection(); 

    public MenClothing(string text) 
    { 
     InitializeComponent(); 
     txtUsername.Text = text; 
     connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Teronkee\Desktop\OFFICAL STAC\StacProductions\DatabaseSaveItem.accdb"; 
    } 

    public MenClothing() 
    { 
     InitializeComponent(); 


     connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Teronkee\Desktop\OFFICAL STAC\StacProductions\DatabaseSaveItem.accdb"; 


    } 


    private int upperCase(string pass) 
    { 
     int num = 0; 
     foreach (char ch in pass) 
     { 
      if (char.IsUpper(ch)) 
      { 
       num++; 
      } 
     } 
     return num; 
    } 

    private void btnlogout_Click(object sender, EventArgs e) 
    { 
     this.Hide(); 
     Form2 Return = new Form2(txtUsername.Text); 
     Return.ShowDialog(); 
    } 

    private void MenClothing_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      connect.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connect; 
      string query = "select * from RegisterItem"; 
      command.CommandText = query; 

      OleDbDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       comboBox1.Items.Add(reader["Name"].ToString()); 
      } 

      connect.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 
     } 

     connect.Close(); 
    } 
    private void pictureBox1_Click(object sender, EventArgs e) 
    { 
     pictureBox1.ImageLocation = ItemUrl.Text; 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     pictureBox1.ImageLocation = ItemUrl.Text; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     try { 
     connect.Open(); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connect; 
     command.CommandText = "insert into RegisterItem([Name], [Url],[Description], [Price]) values('" + ItemName.Text + "','" + ItemUrl.Text + "','" + ItemDescription.Text + "','" + ItemPrice.Text + "')"; 

      string str = ItemUrl.Text; 
      pictureBox1.ImageLocation = str; 
      string str1 = textBox1.Text; 
      Image img = Image.FromFile(str); 
      pictureBox1.Image = img; 

      command.ExecuteNonQuery(); 
      MessageBox.Show("Data Saved"); 

      txtID1.Text = txtUsername.Text; 
       txtName1.Text = ItemName.Text; 
       txtDescription1.Text = ItemDescription.Text; 
       txtPrice1.Text = ItemPrice.Text; 

       ItemName.Text = ""; 
       ItemDescription.Text = ""; 
       ItemPrice.Text = ""; 

      connect.Close(); 
     } 

     catch (Exception ex) 

     { 
     MessageBox.Show("Error " +ex); 

     } 

     connect.Close(); 

    } 

    private void label5_Click(object sender, EventArgs e) 
    { 

    } 

    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox5_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void btnDelete_Click(object sender, EventArgs e) 
    { 

     try 
     { 
      connect.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connect; 
      string query = "delete from RegisterItem where Name ="     +txtName1.Text+""; 
      command.CommandText = query; 


      command.ExecuteNonQuery(); 
      MessageBox.Show("Data deleted "); 

      connect.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 

     } 
     connect.Close(); 

     //if (txtUsername.Text == txtID1.Text) 
     // { 
     // txtName1.Text = ""; 
     // txtDescription1.Text = ""; 
     // txtPrice1.Text = ""; 
     // } 
    } 
    private void ItemDescription_TextChanged(object sender, EventArgs e) 
    { 
    } 
    private void AddString_Click(object sender, EventArgs e) 
    { 
     string namestr = txtName1.Text; 
     comboBox1.Items.Add(namestr); 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      connect.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connect; 
      string query = "select * from RegisterItem where Name='" + comboBox1.Text + "'"; 
      command.CommandText = query; 

      OleDbDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 


       txtName1.Text = reader["Name"].ToString(); 
       txtDescription1.Text = reader["Description"].ToString(); 
       txtPrice1.Text = reader["Price"].ToString(); 
      } 

      connect.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 
     } 

     connect.Close(); 
    } 
} 

}

+0

它的工作原理,當我走進ID交換的名稱(第二行)(第一行) ,但沒有與我想要的名稱 – TeaCake

回答

0

您的查詢沒有被正確引用的列。 ID是一個數字,因此它不需要單引號,Name是一個字符串 - 它的確如此。

這就是爲什麼ID作品和名稱不。 您刪除查詢更改爲

string query = "delete from RegisterItem where Name = '" + txtName1.Text + "'"; 

你應該使用的String.Format(),以避免這些錯誤,更好的使用paramaterized queries

+0

感謝您爲我解決這個問題,我遇到的另一個錯誤是錯誤System.NotSupportedException:不支持給定路徑的格式。點擊button1後,是否因爲我用來將url轉換爲圖像的代碼?我是否把它放在button1的錯誤位置? @Stavm – TeaCake

+0

@TeaCake這個錯誤通常意味着一個文件的路徑無效 - 我不知道ItemUrl中的值是什麼,但它可能是您想要查看的位置,具體取決於您得到異常的哪一行。 順便說一句,我相信你的問題已經得到解答,所以用答案左邊的V來標記這個問題爲'已解決'將被認爲是適當的行爲。 如果您有任何問題可隨時打開一個新的問題 – Stavm

相關問題