Пример #1
0
        void folio_pedido() // Método para mostrar el folio siguiente al último registrado en la base de datos
        {
            int             folio     = 0;
            MySqlConnection conexion  = Conectar();
            String          sentencia = "SELECT IDpedido FROM pedido ORDER BY IDpedido DESC LIMIT 1";
            MySqlCommand    comando   = new MySqlCommand(sentencia, conexion);

            conexion.Open();
            comando.ExecuteNonQuery();
            MySqlDataReader MyReader;

            MyReader = comando.ExecuteReader();
            while (MyReader.Read())
            {
                folio = (MyReader.GetInt16("IDpedido"));
                folio = folio + 1;
            }
            if (folio == 0)
            {
                folio = 1;
            }
            MyReader.Close();
            conexion.Close();
            var folio_padded = folio.ToString().PadLeft(4, '0');

            textBox_idPedido.Text = folio_padded;
        }
Пример #2
0
        private void agregarPedido_Click(object sender, EventArgs e) // Leer datos ingresados por el usuario e insertar un pedido a la base de datos
        {
            //Recuperar id del cliente
            int             IDcliente = 0;
            int             IDpedido  = 0;
            MySqlConnection conexion  = Conectar();
            String          sentenciaBuscarCliente = "SELECT IDcliente FROM clientes WHERE nombre = '" + textBox_cliente.Text + "'";
            MySqlCommand    comando1 = new MySqlCommand(sentenciaBuscarCliente, conexion);

            conexion.Open();
            comando1.ExecuteNonQuery();
            MySqlDataReader MyReader;

            MyReader = comando1.ExecuteReader();
            while (MyReader.Read())
            {
                IDcliente = MyReader.GetInt16("IDcliente");
            }
            MyReader.Close();

            //Insertar en tabla pedido y recuperar el ID insertado
            String       sentenciaPedido = "INSERT INTO pedido (IDcliente, total, fecha_pedido, fecha_entrega ) VALUES " + " (" + IDcliente + "," + "" + textBox_total.Text.Substring(1, textBox_total.Text.Length - 1) + "," + "'" + fechaPedido.Value.ToString("yyyy-MM-dd") + "'," + "'" + fechaEntrega.Value.ToString("yyyy-MM-dd") + "');";
            String       pedidoInsertado = "SELECT LAST_INSERT_ID() ";
            MySqlCommand comando         = new MySqlCommand(sentenciaPedido, conexion);

            comando.ExecuteNonQuery();
            MySqlCommand comando5 = new MySqlCommand(pedidoInsertado, conexion);

            comando5.ExecuteNonQuery();
            MyReader = comando5.ExecuteReader();
            while (MyReader.Read())
            {
                IDpedido = Int32.Parse(MyReader.GetString(0));
            }
            MyReader.Close();

            //Insertar en tabla pedido_producto por cada producto regstrado por el usuario
            for (int i = 1; i < cantidad_productos + 1; i++)
            {
                String sentencia4 = "";
                String sentencia3 = "";
                int    IDproducto = 0;

                if (i == 1)
                {
                    sentencia4 = "SELECT IDproducto FROM producto WHERE nombre = '" + comboBox_producto1.Text + "'";
                    MySqlCommand comando2 = new MySqlCommand(sentencia4, conexion);
                    comando2.ExecuteNonQuery();
                    MyReader = comando2.ExecuteReader();
                    while (MyReader.Read())
                    {
                        IDproducto = Int32.Parse(MyReader.GetString(0));
                    }
                    MyReader.Close();
                    sentencia3 = "INSERT INTO pedido_producto (IDpedido, IDproducto, cantidad, precio) VALUES " + " (" + IDpedido + ", " + "" + IDproducto + "," + "'" + textBox_cantidad1.Text + "'," + "'" + textBox_precio1.Text.Substring(1, textBox_precio1.Text.Length - 1) + "')";
                }
                else if (i == 2)
                {
                    sentencia4 = "SELECT IDproducto FROM producto WHERE nombre = '" + comboBox_producto2.Text + "'";
                    MySqlCommand comando2 = new MySqlCommand(sentencia4, conexion);
                    comando2.ExecuteNonQuery();
                    MyReader = comando2.ExecuteReader();
                    while (MyReader.Read())
                    {
                        IDproducto = Int32.Parse(MyReader.GetString(0));
                    }
                    MyReader.Close();
                    sentencia3 = "INSERT INTO pedido_producto (IDpedido, IDproducto, cantidad, precio) VALUES " + " (" + IDpedido + ", " + "" + IDproducto + "," + "'" + textBox_cantidad2.Text + "'," + "'" + textBox_precio2.Text.Substring(1, textBox_precio2.Text.Length - 1) + "')";
                }
                else if (i == 3)
                {
                    sentencia4 = "SELECT IDproducto FROM producto WHERE nombre = '" + comboBox_producto3.Text + "'";
                    MySqlCommand comando2 = new MySqlCommand(sentencia4, conexion);
                    comando2.ExecuteNonQuery();
                    MyReader = comando2.ExecuteReader();
                    while (MyReader.Read())
                    {
                        IDproducto = Int32.Parse(MyReader.GetString(0));
                    }
                    MyReader.Close();
                    sentencia3 = "INSERT INTO pedido_producto (IDpedido, IDproducto, cantidad, precio) VALUES " + " (" + IDpedido + ", " + "" + IDproducto + "," + "'" + textBox_cantidad3.Text + "'," + "'" + textBox_precio3.Text.Substring(1, textBox_precio3.Text.Length - 1) + "')";
                }
                else if (i == 4)
                {
                    sentencia4 = "SELECT IDproducto FROM producto WHERE nombre = '" + comboBox_producto4.Text + "'";
                    MySqlCommand comando2 = new MySqlCommand(sentencia4, conexion);
                    comando2.ExecuteNonQuery();
                    MyReader = comando2.ExecuteReader();
                    while (MyReader.Read())
                    {
                        IDproducto = Int32.Parse(MyReader.GetString(0));
                    }
                    MyReader.Close();
                    sentencia3 = "INSERT INTO pedido_producto (IDpedido, IDproducto, cantidad, precio) VALUES " + " (" + IDpedido + ", " + "" + IDproducto + "," + "'" + textBox_cantidad4.Text + "'," + "'" + textBox_precio4.Text.Substring(1, textBox_precio4.Text.Length - 1) + "')";
                }
                else if (i == 5)
                {
                    sentencia4 = "SELECT IDproducto FROM producto WHERE nombre = '" + comboBox_producto5.Text + "'";
                    MySqlCommand comando2 = new MySqlCommand(sentencia4, conexion);
                    comando2.ExecuteNonQuery();
                    MyReader = comando2.ExecuteReader();
                    while (MyReader.Read())
                    {
                        IDproducto = Int32.Parse(MyReader.GetString(0));
                    }
                    MyReader.Close();
                    sentencia3 = "INSERT INTO pedido_producto (IDpedido, IDproducto, cantidad, precio) VALUES " + " (" + IDpedido + ", " + "" + IDproducto + "," + "'" + textBox_cantidad5.Text + "'," + "'" + textBox_precio5.Text.Substring(1, textBox_precio5.Text.Length - 1) + "')";
                }
                else if (i == 6)
                {
                    sentencia4 = "SELECT IDproducto FROM producto WHERE nombre = '" + comboBox_producto6.Text + "'";
                    MySqlCommand comando2 = new MySqlCommand(sentencia4, conexion);
                    comando2.ExecuteNonQuery();
                    MyReader = comando2.ExecuteReader();
                    while (MyReader.Read())
                    {
                        IDproducto = Int32.Parse(MyReader.GetString(0));
                    }
                    MyReader.Close();
                    sentencia3 = "INSERT INTO pedido_producto (IDpedido, IDproducto, cantidad, precio) VALUES " + " (" + IDpedido + ", " + "" + IDproducto + "," + "'" + textBox_cantidad6.Text + "'," + "'" + textBox_precio6.Text.Substring(1, textBox_precio6.Text.Length - 1) + "')";
                }
                else if (i == 7)
                {
                    sentencia4 = "SELECT IDproducto FROM producto WHERE nombre = '" + comboBox_producto7.Text + "'";
                    MySqlCommand comando2 = new MySqlCommand(sentencia4, conexion);
                    comando2.ExecuteNonQuery();
                    MyReader = comando2.ExecuteReader();
                    while (MyReader.Read())
                    {
                        IDproducto = Int32.Parse(MyReader.GetString(0));
                    }
                    MyReader.Close();
                    sentencia3 = "INSERT INTO pedido_producto (IDpedido, IDproducto, cantidad, precio) VALUES " + " (" + IDpedido + ", " + "" + IDproducto + "," + "'" + textBox_cantidad7.Text + "'," + "'" + textBox_precio7.Text.Substring(1, textBox_precio7.Text.Length - 1) + "')";
                }
                else if (i == 8)
                {
                    sentencia4 = "SELECT IDproducto FROM producto WHERE nombre = '" + comboBox_producto8.Text + "'";
                    MySqlCommand comando2 = new MySqlCommand(sentencia4, conexion);
                    comando2.ExecuteNonQuery();
                    MyReader = comando2.ExecuteReader();
                    while (MyReader.Read())
                    {
                        IDproducto = Int32.Parse(MyReader.GetString(0));
                    }
                    MyReader.Close();
                    sentencia3 = "INSERT INTO pedido_producto (IDpedido, IDproducto, cantidad, precio) VALUES " + " (" + IDpedido + ", " + "" + IDproducto + "," + "'" + textBox_cantidad8.Text + "'," + "'" + textBox_precio8.Text.Substring(1, textBox_precio8.Text.Length - 1) + "')";
                }

                MySqlCommand comando3 = new MySqlCommand(sentencia3, conexion);
                comando3.ExecuteNonQuery();
            }

            conexion.Close();
            MessageBox.Show("Se ha agregado un pedido");
            comboBox_producto1.SelectedIndex = -1;
            textBox_cliente.Clear();
            textBox_cantidad1.Clear();
            folio_pedido();
        }