Stock Quantity Management C#

Asked

Viewed 305 times

0

I cannot find a solution for stock and sales management.

The scenario would be a stock of Auto parts, where I register the entry of a product in the stock with a certain amount.

Cod - name - Qtd - vlr

3239 - Water pump - 2 - R$300,00

and soon I want to add 3 more quantities of the same item totaling 5 items of the Water Pump, or reducing quantities as sales are made.

maybe some IF, FOR, COLLECTION

Ex:
   If (cod == cod)  
     {  
       ++1 qtd  
     }

I have an Access database, I am accessing directly in the database without being Dataset:

An example of how I’m recording the data today in db:

        OleDbConnection Con = new OleDbConnection();
        Con.ConnectionString = Properties.Settings.Default.dbqtd;

        Con.Open();
        OleDbCommand Cmm = new OleDbCommand();
        Cmm.CommandText = "INSERT INTO estoque (codproduto, nome, qtd, local, numero) VALUES (?, ?, ?, ?, ?) ";
        Cmm.Parameters.Clear();

        Cmm.Parameters.Add("@codproduto", OleDbType.VarChar, 50).Value = txtCodProd.Text;
        Cmm.Parameters.Add("@nome", OleDbType.VarChar, 50).Value = txtNome.Text;
        Cmm.Parameters.Add("@qtd", OleDbType.VarChar,50).Value = txtQtd.Text;
        Cmm.Parameters.Add("@local", OleDbType.VarChar, 50).Value = txtLocal.Text;
        Cmm.Parameters.Add("@numero", OleDbType.VarChar, 50).Value = txtNumero.Text;
        
        Cmm.CommandType = CommandType.Text;
        Cmm.Connection = Con;

        Cmm.ExecuteNonQuery();
        MessageBox.Show("Inclusão efetuada com Sucesso !");
  • 4

    Use the operator +=, ex.: qtd += 3

  • @zekk I could not make it work, or I did not understand how it does. could put a more complete explanation please ?

  • You want to increment before or after saving to the bank?

  • 1

    The situation here is as follows: 1 initially. if later I need to insert this same item and it still contains 1 in my stock, I would like it not to duplicate but to transform the value into 2.

  • 1

    You can consult the quantity of such product in a select ..., by the number in a variable x and check that it is equal to 1, if it is, you increase x thus: x += 1, and then you update the field qtd of that product with the variable x.

  • 1

    @Mauritosanches, as the zekk said, you have to check if it contains the product in the table. If the product exists you update the quantity with the UPDATE command, otherwise you insert this product in the table with the quantities you want using the INSERT command.

Show 1 more comment

1 answer

0


Good morning Guys, I was able to solve the problem according to what Zekk and Diego said.

(I worked the sum of the amount in hidden Textboxs, so I didn’t have to add in variables).

Follows:

  private void btnInclude_Click(object sender, EventArgs e)
    {
        OleDbConnection Con = new OleDbConnection();
        Con.ConnectionString = Properties.Settings.Default.dbqtd;

        string qtdvar = "";

        Con.Open();

        OleDbCommand Cmm = new OleDbCommand();
        Cmm.CommandText = "SELECT qtd FROM estoque WHERE codproduto = '" + codprod.Text + "' ";
        Cmm.CommandType = CommandType.Text;
        Cmm.Connection = Con;

        qtdvar = Convert.ToString(Cmm.ExecuteScalar());

        Con.Close();

        qtdestoque.Text = qtdvar;

        if (qtdvar == "")
        {
            // QTD TEM VALOR

            OleDbConnection Coninsert = new OleDbConnection();
            Coninsert.ConnectionString = Properties.Settings.Default.dbqtd;

            Coninsert.Open();
            OleDbCommand cmminsert = new OleDbCommand();
            cmminsert.CommandText = "INSERT INTO estoque (codproduto, nome, qtd, location, numero) VALUES (?, ?, ?, ?, ?) ";
            cmminsert.Parameters.Clear();

            cmminsert.Parameters.Add("@codproduto", OleDbType.VarChar, 50).Value = codprod.Text;
            cmminsert.Parameters.Add("@nome", OleDbType.VarChar, 50).Value = txtNome.Text;
            cmminsert.Parameters.Add("@qtd", OleDbType.Integer, 18).Value = txtQtd.Text;
            cmminsert.Parameters.Add("@local", OleDbType.VarChar, 50).Value = txtLocal.Text;
            cmminsert.Parameters.Add("@numero", OleDbType.VarChar, 50).Value = txtNumero.Text;

            cmminsert.CommandType = CommandType.Text;
            cmminsert.Connection = Coninsert;

            cmminsert.ExecuteNonQuery();
            MessageBox.Show("Inclusão efetuada com Sucesso !");

            //===== Recarregar DataGrid
            OleDbConnection Con1 = new OleDbConnection();
            Con1.ConnectionString = Properties.Settings.Default.dbqtd;

            Con1.Open();

            OleDbCommand Cmm1 = new OleDbCommand();
            Cmm1.CommandText = "SELECT * FROM estoque";
            Cmm1.CommandType = CommandType.Text;
            Cmm1.Connection = Con1;

            OleDbDataReader DR1;
            DR1 = Cmm1.ExecuteReader();
            DataTable dt1 = new DataTable();
            dt1.Load(DR1);
            dataGridView2.DataSource = dt1;
            dataGridView2.Refresh();

            Con1.Close();

        }
        else
        {
         //========= QTD TEM VALOR


            qtdresult.Text = Convert.ToString(Convert.ToInt32(qtd.Text) + Convert.ToInt32(qtdestoque.Text));


            OleDbConnection Conupdate = new OleDbConnection();
            Conupdate.ConnectionString = Properties.Settings.Default.dbqtd;


            Conupdate.Open();
            OleDbCommand Cmmupdate = new OleDbCommand();
            Cmmupdate.CommandText = "UPDATE estoque SET qtd = ? WHERE codproduto = ?    ";
            Cmmupdate.Parameters.Clear();

            Cmmupdate.Parameters.Add("@qtd", OleDbType.Integer, 18).Value = Convert.ToInt32(qtdresult.Text);
            Cmmupdate.Parameters.Add("@codprod", OleDbType.VarChar, 50).Value = codprod.Text;



            Cmmupdate.CommandType = CommandType.Text;
            Cmmupdate.Connection = Conupdate;

            Cmmupdate.ExecuteNonQuery();
            MessageBox.Show("Alteração efetuada com Sucesso !");

            Conupdate.Close();


            //===== Recarregar DataGrid
            OleDbConnection Con1 = new OleDbConnection();
            Con1.ConnectionString = Properties.Settings.Default.dbqtd;

            Con1.Open();

            OleDbCommand Cmm1 = new OleDbCommand();
            Cmm1.CommandText = "SELECT * FROM estoque";
            Cmm1.CommandType = CommandType.Text;
            Cmm1.Connection = Con1;

            OleDbDataReader DR1;
            DR1 = Cmm1.ExecuteReader();
            DataTable dt1 = new DataTable();
            dt1.Load(DR1);
            dataGridView2.DataSource = dt1;
            dataGridView2.Refresh();

            Con1.Close();

        }
      }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.