C# .NET - Updating data to the database won't work

Asked By Fuhans Puji Saputra on 27-Aug-13 10:36 AM

I got the problem. I want to update the data to the database, but the database won't update.

Here is the code:

else if (firstForm.textBox1.Text == "Seranne")
      {
        string query = "SELECT [Quantity], [Description], [Price] FROM [Seranne] WHERE [Code] IN (";
 
        OleDbConnection conn = new OleDbConnection(connectionString);
 
        conn.Open();
 
        if (int.TryParse(this.textBoxCodeContainer[0].Text, out codeValue))
        {
          query = query + codeValue.ToString();
        }
 
        for (int i = 1; i < 17; i++)
        {
          if (int.TryParse(this.textBoxCodeContainer[i].Text, out codeValue))
          {
            query = query + "," + codeValue.ToString();
          }
        }
 
        query = query + ")";
 
        OleDbCommand cmd = new OleDbCommand(query, conn);
 
        cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
        cmd.Parameters.Add("Quantity", System.Data.OleDb.OleDbType.Integer);
 
        OleDbDataReader dReader;
 
        dReader = cmd.ExecuteReader();
 
        while (dReader.Read())
        {
          if (textBoxCodeContainer[index].TextLength != 0)
          {
            this.textBoxQuantityContainer[index].Maximum = Convert.ToDecimal(dReader["Quantity"].ToString());
            this.textBoxDescContainer[index].Text = dReader["Description"].ToString();
            this.textBoxSubTotalContainer[index].Text = dReader["Price"].ToString();
          }
 
          if (textBoxQuantityContainer[index].Value != 0 && textBoxQuantityContainer[index].Value >= Convert.ToDecimal(dReader["Quantity"].ToString()))
          {
            newVal = textBoxQuantityContainer[index].Value - Convert.ToDecimal(dReader["Quantity"].ToString());
            cmd = new OleDbCommand("UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN ('");
          }
 
          index += 1;
        }
 
        conn.Close();
        dReader.Close();
      }
    }
 
    private void UpdateQuantity()
    {
      System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
      sound.Play();
      MessageBox.Show("Updated Successfully", "Success");
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
      UpdateQuantity();
    }

Above code all worked, excepts for updating the Quantity to the database. What I mean is, I set theQuantity in database to 100, when I set Quantity to 10 in my program and update it, the database should be update the Quantity to 90 (because 100 - 10), but it is still at 100.

Could I wrong somewhere?

Here is the link of the screenshots: (ScreenShot 1)https://www.dropbox.com/s/rph5iuh371rc9ny/Untitled.png

(ScreenShot 2) https://www.dropbox.com/s/5q8pyztqy7ejupy/Capture.PNG

In the Screenshot 1, I already set the quantity to 10 and the messagebox show that the data has been updated successfully and the data in the database supposed to be 90 (because 100-10). But, in the Screenshot 2 where the database is, the Quantity still at 100.

Thanks in advance!

chiqu thakur replied to Fuhans Puji Saputra on 03-Sep-13 08:26 AM
SNR INFOCOM is a Multidimension Developing Company in Noida (Delhi NCR) which has stood apart from crowd for years with superior quality work, quick turn-around-time and affordable rates. We are widely known in India and abroad for Software & Web Development, Flash Presentation, Content Writing, Computer Hardware Support, Networking,
Eric Smith replied to Fuhans Puji Saputra on 03-Sep-13 04:47 PM
The issue is that your UpdateQuantity() only plays a sound and displays the message. It doesn't contain any code that interactions with your database.