2012-03-10 55 views
0

首先,我想說的是,在過去的幾周裏,這個網站對我來說是一個非常大的幫助。好吧,我已經在C#中編寫了一個窗體應用程序,包含一個用於過濾的文本框,一個列表視圖和兩個文本框以及兩個用於添加新股票和一個用於更新的按鈕。更新SQLCE會導致整列數據發生變化C#

這個想法是打開程序,它顯示所有的股票,你選擇你想編輯的項目,它將值下降到兩個文本框,在那裏你可以修改它們並保存它,或者添加新的股票項目。

我得到了一些關於更新SQLCE數據庫的代碼,它工作正常,但我不明白的是當我試圖在不同的窗體上使用此代碼,更新我的股票列表,它不會工作正常。它會更新整個列而不是所選的行。

我試圖從頭開始寫它,甚至複製工作代碼,但都給出了相同的錯誤。下面是代碼,我使用,它只是更新從兩個文本框值數據庫中的兩個字段,如果股票被上傳錯誤

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms;   
    using System.Data.SqlServerCe; 

    namespace WindowsFormsApplication1 
    { 
    public partial class Form1 : Form 
    { 
    SqlCeConnection cn = new SqlCeConnection("Data source = c:\\Clients.sdf"); 
    string QTYinStock; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      cn.Open(); 
      ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn)); 

     } 

     catch (SqlCeException ex) 
     { 
      MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 
      Application.ExitThread(); 

     } 
    } 
    private void button2_Click(object sender, EventArgs e) 
    { 

     if (checkBox1.Checked == true) 
     { 

      //Check the QTY 
      int QTY; 
      bool isInteger = int.TryParse(textBox2.Text, out QTY); 



      if (isInteger) 
      { 
       SqlCeCommand cm = new SqlCeCommand("INSERT INTO Stock(QTY_in_Stock, Cartridge_Code) VALUES(@QTY_in_Stock, @Cartridge_Code)", cn); 
       cm.Parameters.AddWithValue("@QTY_in_Stock", textBox2.Text); 
       cm.Parameters.AddWithValue("@Cartridge_Code", textBox3.Text); 


       try 
       { 
        int affectedRows = cm.ExecuteNonQuery(); 

        if (affectedRows > 0) 
        { 
         MessageBox.Show("Insert Successfull!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); 
         textBox1.Text = ""; 
         textBox2.Text = ""; 
         textBox3.Text = ""; 
         ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn)); 

        } 

        else 
        { 
         MessageBox.Show("Insert Failed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 

        } 
       } 

       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 


       } 
      } 
      else 
      { 
       MessageBox.Show("No data inserted! The QTY is not valid!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning); 


      } 
     } 
     else 
     { 
      MessageBox.Show("Please ensure all details are correct and check the checkbox"); 
     } 
    } 

    private void Texboxes_TextChanged(object sender, EventArgs e) 
    { 
     if (textBox2.Text != "" && textBox3.Text != "") 
     { 
      button1.Enabled = true; 
      button2.Enabled = true; 

     } 

     else 
     { 
      button1.Enabled = false; 
      button2.Enabled = false; 
     } 
    } 
    public void ReadStock(SqlCeCommand cm) 
    { 

     listView1.Items.Clear(); 

     SqlCeDataReader dr3 = cm.ExecuteReader(); 

     while (dr3.Read()) 
     { 

      ListViewItem it3 = new ListViewItem(dr3["QTY_in_Stock"].ToString()); 
      it3.SubItems.Add(dr3["Cartridge_Code"].ToString()); 




      listView1.Items.Add(it3); 
     } 
     dr3.Close(); 
     dr3.Dispose(); 

    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     ReadStock(new SqlCeCommand("SELECT * FROM Stock WHERE Cartridge_Code LIKE '%" + textBox1.Text + "%' ORDER BY Cartridge_Code", cn)); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (checkBox1.Checked == true) 
     { 

      int QTY; 
      bool isInteger = int.TryParse(textBox2.Text, out QTY); 

      if (isInteger) 
      { 
       SqlCeCommand cm = new SqlCeCommand("UPDATE Stock SET QTY_in_Stock = @QTY_in_Stock, Cartridge_Code = @Cartridge_Code WHERE QTY_in_Stock = '" + QTYinStock +"'", cn); 
       cm.Parameters.AddWithValue("@QTY_in_Stock", textBox2.Text); 
       cm.Parameters.AddWithValue("@Cartridge_Code", textBox3.Text); 



       try 
       { 
        cm.ExecuteNonQuery(); 
        ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn)); 

        textBox2.Text = textBox3.Text = ""; 


       } 
       catch (Exception ex) 
       { 

        MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 


       } 

       checkBox1.Checked = false; 
      } 
      else 
      { 

       MessageBox.Show("The QTY Is Not Valid", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning); 

      } 
     } 
     else 
     { 
      MessageBox.Show("Please ensure all Data is correct and checkbox is Checked"); 
     } 
    } 

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (listView1.SelectedItems.Count > 0) 
     { 

      QTYinStock = textBox2.Text = listView1.SelectedItems[0].Text; 
      textBox3.Text = listView1.SelectedItems[0].SubItems[1].Text; 


     } 

     else 
     { 
      textBox2.Text = textBox3.Text = ""; 


     } 
    } 
    } 
    } 

對不起張貼整個代碼,我知道它不是整齊地碼,我只編寫了大約一個月,如果這只是一個語法錯誤,請你指出來,因爲我已經盯着這個了將近一天,不能弄明白,或者做了這麼多的其他想法讚賞。

回答

0

我懷疑QTYinStock匹配Stock表中的多個記錄。 Stock表應該有一個自動遞增整數值的主鍵設置,因此您可以唯一標識每條記錄。

相反的:

UPDATE編組QTY_in_Stock = @QTY_in_Stock,Cartridge_Code = @Cartridge_Code WHERE QTY_in_Stock =「」 + QTYinStock + 「'」

你可以使用:

UPDATE Stock SET QTY_in_Stock = @QTY_in_Stock, Cartridge_Code = @Cartridge_Code WHERE StockID = " + stockID 

stockID是保存在變量中的唯一行標識符您需要在加載r時退出該值ecords。您還應該考慮使WHERE子句中的變量成爲@QTY_in_Stock和@Cartridge_Code值的參數。

相關問題