Stock Quantity Management C #

0

I can not find a solution for inventory and sales management.

The scenario would be a Stock of Autoparts, where I register the entry of a product into stock with a certain quantity.

  

cod - name - qtd - vlr

     

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

and soon I want to add another 3 quantities of the same item totaling 5 Water Pump items, 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 am writing 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 !");
    
asked by anonymous 10.08.2016 / 18:37

1 answer

0

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

(I worked the sum of the number in Hidden Textboxes, so I did not have to add them 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();

        }
      }
    
15.08.2016 / 14:38