Hai everyone, i want to minus the data from the database with the value that i give when i run the program, everything is works, but i keep stuck at the newVal i already did like this, but the newVal keep appear 0 (because i declared decimal newVal = 0, but on this thread, i just used decimal newVal;), and 2 more problems: if i move the newVal = ... at the top, it is useless, because the one of the calculations in the newVal is reading data from the database (since i want database minus with the new value given when i run the program), but if i put the newVal at the bottom after reading data, it is useless also, because i set the Quantity = ? and the value of ? is newVal.. Well, here is the code:

private void UpdateQuantity()
        {
            int index = 0;
            int codeValue = 0;
            decimal newVal;

            List<int> integers = new List<int>();

            foreach (var tb in textBoxCodeContainer)
            {
                if (int.TryParse(tb.Text, out codeValue))
                {
                    integers.Add(codeValue);
                }
            }

            string command = "UPDATE [Seranne] SET [Quantity]=? WHERE [Code] IN(" + string.Join(", ", integers) + ")";

            OleDbConnection conn = new OleDbConnection(connectionString);

            OleDbCommand cmd = new OleDbCommand(command, conn);

            cmd.Parameters.Add("Quantity", System.Data.OleDb.OleDbType.Integer);
            cmd.Parameters["Quantity"].Value = this.newVal.ToString();

            OleDbDataReader dReader;

            conn.Open();

            dReader = cmd.ExecuteReader();

            while (dReader.Read())
            {
                if (textBoxQuantityContainer[index].Value != 0 && textBoxQuantityContainer[index].Value >= Convert.ToInt32(dReader["Quantity"].ToString()))
                {
                    newVal = (Convert.ToInt32(dReader["Quantity"].ToString()) - textBoxQuantityContainer[index].Value);

                    int numberOfRows = cmd.ExecuteNonQuery();
                }

                index += 1;
            }

            if (newVal == 0)
            {
                System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                sounds.Play();
                MessageBox.Show("Cannot Update", "Error");
            }

            else
            {
                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                sound.Play();
                MessageBox.Show("Was Updated Successfully", "Success");
            }

            dReader.Close();
            conn.Close();
        }

Could you help me guys? i already thinking another way, but it is useless also, keep stuck at the newVal, since it is read the data from the database and required dReader = cmd.ExecuteReader();

Thanks a bunch

Edited 3 Years Ago by fuhanspujisaputra

if iam right i think you didnt set the value of newVal after you initialize it.

and one thing i think you need a select query.

No, i already set the value of newVal after i declared it, but it is on below right after i declared it. Because, one of the statement in the newVal is required dataReader, and also the dataReader is required the commandText (UPDATE), so i put it in below right after those requirements declared by me. So, what should i do in order to get the right value of newVal? Not as 0

This question has already been answered. Start a new discussion instead.