简体   繁体   中英

Minus data from the program to the database

How do i minus data from the program to the database?

I have quantity and description in my database. The quantity will be 100, and the description of it will be A.

When i type in my program like this (after run the program): The quantity is 50. and the description of it is A.

The database: The quantity will be 50 (because minus with the database and my program "100 - 50 = 50", and the description still remains same as A.

How do i do that?

Thanks in advance!

Edit:

I already did like this:

if (textBoxCodeContainer[index].TextLength != 0)
                    {
                        this.textBoxQuantityContainer[index].Value = Convert.ToDecimal(dReader["Quantity"].ToString());
                        this.textBoxDescContainer[index].Text = dReader["Description"].ToString();
                        this.textBoxSubTotalContainer[index].Text = dReader["Price"].ToString();
                    }

                    if (textBoxQuantityContainer[index].Value != 0)
                    {
                        if (textBoxQuantityContainer[index].Value >= Convert.ToDecimal(dReader["Quantity"].ToString()))
                        {
                             decimal newVal = textBoxQuantityContainer[index].Value - Convert.ToDecimal(dReader["Quantity"].ToString());
                             cmd = new OleDbCommand("UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN (");
                        }
                    }

But when i run the program, the quantity loaded exactly the same like in the database, but it can't change, when i type quantity is 50, the program will automatically return to 100 (which is same exactly the quantity in the database)

Why is it like that?

Note: for textbox quantity, i use the Numeric Up Down.

Edit: The full code is on below:

        private void UpdateDatas()
        {
            int codeValue = 0;
            int index = 0;

            if (firstForm.textBox1.Text == "Seranne")
            {
                string query = "SELECT [Quantity], [Description], [Price] FROM [Seranne] WHERE [Code] IN (";

                OleDbDataReader dReader;
                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);

                dReader = cmd.ExecuteReader();

                while (dReader.Read())
                {
                    if (textBoxCodeContainer[index].TextLength != 0)
                    {
                        this.textBoxQuantityContainer[index].Value = Convert.ToDecimal(dReader["Quantity"].ToString());
                        this.textBoxDescContainer[index].Text = dReader["Description"].ToString();
                        this.textBoxSubTotalContainer[index].Text = dReader["Price"].ToString();
                    }

                    if (textBoxQuantityContainer[index].Value != 0)
                    {
                        if (textBoxQuantityContainer[index].Value >= Convert.ToDecimal(dReader["Quantity"].ToString()))
                        {
                             decimal newVal = textBoxQuantityContainer[index].Value - Convert.ToDecimal(dReader["Quantity"].ToString());
                             cmd = new OleDbCommand("UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN (");
                        }
                    }

                    index += 1;
                }

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

        private void UpdatePrice()
        {
            int totalPrice = 0;
            int quantity = 0;
            int price = 0;

            for (int i = 0; i < 17; i++)
            {
                if (textBoxQuantityContainer[i].Value > 0)
                {
                    quantity = (int)textBoxQuantityContainer[i].Value;
                    price = Convert.ToInt32(textBoxSubTotalContainer[i].Text);
                    textBoxTotalContainer[i].Text = (quantity * price).ToString();
                }

                else
                {
                    textBoxSubTotalContainer[i].Text = "";
                    textBoxTotalContainer[i].Text = "";
                }
            }

            for (int i = 0; i < 17; i++)
            {
                if (textBoxTotalContainer[i].TextLength != 0)
                {
                    totalPrice += Convert.ToInt32(textBoxTotalContainer[i].Text);
                }
            }

            textBoxAllTotalContainer.Text = totalPrice.ToString("n2");
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            UpdateDatas();
            UpdatePrice();
        }

    }
}

Here is the screenshot: 在此处输入图片说明在此处输入图片说明

"Code" displayed in the screenshot above is refer to the database, and also the "Quantity", whenever i type the code that already have in the database, the remaining box are filled up. But, when i change the "Quantity" from 100 to 50, it is automatically like refresh the program to back to 100 again.

 cmd = new oledbcommand("select qty from tablename where pid=103");
qtyval = convert.ToInt32(cmd.ExecuteScalar());
if (qtyval != 0 )
{
if (qtyval >= convert.toint32(txtqty.text))
{
newval = qtyval - convert.ToInt32(txtqty.text);
cmd = new oledbcommand("update tablename set qty="+newval+" where pid=103");
}
}
cmd = new OleDbCommand("UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN (");

[This can't be your actual code because it is missing a closing quote and bracket. Please always just copy and paste your actual code.]

WHERE [Code] IN (")

You are attempting to update where the Code is an empty string? It is far more likely to be NULL, so this is one reason why it won't update. However, you haven't executed this cmd, so it won't change anything.

[Do you really intend to update ALL records which don't have a Code ?]

You should also print-out cmd and attempt to execute it from within Access as part of your debugging steps.

Lastly, why are you quoting the Quantity with apostrophes? Presumably, it is a numeric field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM