datagridview does not fill in the fields (Windows Form) - C # - Visual Studio Comunnity 2015

-1

Hello, I have a problem, I have the following problem: When I enter the data in the datagridview it does not "pull" the information to put on the screen. The DB is MySQL, Localhost and there are no errors. Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace SistemaVendas
{
    public partial class Caixa : Form
    {
        public Caixa()
        {
            InitializeComponent();
        }
        int precototal = 0;
        MySqlConnection con = new MySqlConnection("server=localhost; database=sistemacsharp; username=root; password=");


        private void txtnumprod_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==13)
            {
                txtquantidade.Enabled = true;
                txtquantidade.Focus();
            }
        }

        private void txtquantidade_KeyPress(object sender, KeyPressEventArgs e)

        {
            txtnumprod.Clear();
            txtquantidade.Clear();
            try
            {
                string txt = "SELECT * FROM produtos WHERE ID='" + txtnumprod + "'";
                MySqlCommand cmd = new MySqlCommand(txt, con);
                con.Open();
                MySqlDataReader r = cmd.ExecuteReader();
                while (r.Read())
                {
                    int preco = int.Parse(txtquantidade.Text.ToString()) * int.Parse(r[6].ToString());
                    precototal = preco;
                    dataGridView1.Rows.Insert(dataGridView1.RowCount, r[0], r[1], txtquantidade.Text.Trim(), r[6], preco);
                }
                lbltotalitens.Text = "" + (dataGridView1.RowCount - 1) + "";
                lbltotal.Text = "" + precototal + "";
                con.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message,"Este É Um Erro Vindo Do Banco de Dados");
            }
            txtnumprod.Focus();
            txtnumprod.Clear();
            txtquantidade.Enabled = false;
            txtquantidade.Clear();
        }
    }
}

    
asked by anonymous 09.06.2018 / 05:05

1 answer

0

Your code has several problems, first of which is what Rovann quoted, the clear field of the quantity before being used.

Second, if you have any problems with your code, the connection will not be closed and you will not be giving it to the objects.

Note that I'm using the "using" and I've deleted the "Clear" before using them (because it did not make any sense).

Try this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace SistemaVendas
{
    public partial class Caixa : Form
    {
        public Caixa()
        {
            InitializeComponent();
        }
        int precototal = 0;
        MySqlConnection con = new MySqlConnection("server=localhost; database=sistemacsharp; username=root; password=");


        private void txtnumprod_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==13)
            {
                txtquantidade.Enabled = true;
                txtquantidade.Focus();
            }
        }

        private void txtquantidade_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                string txt = "SELECT * FROM produtos WHERE ID = @NumProd";

                using (MySqlCommand cmd = new MySqlCommand(txt, con))
                {
                    cmd.Parameters.AddWithValue("@NumProd", txtnumprod.Text);
                    con.Open();
                    using (MySqlDataReader r = cmd.ExecuteReader())
                    {
                        while (r.Read())
                        {
                            int preco = int.Parse(txtquantidade.Text.ToString()) * int.Parse(r[6].ToString());
                            precototal = preco;
                            dataGridView1.Rows.Insert(dataGridView1.RowCount, r[0], r[1], txtquantidade.Text.Trim(), r[6], preco);
                        }
                        lbltotalitens.Text = "" + (dataGridView1.RowCount - 1) + "";
                        lbltotal.Text = "" + precototal + "";
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message,"Este É Um Erro Vindo Do Banco de Dados");
            }
            txtnumprod.Focus();
            txtnumprod.Clear();
            txtquantidade.Enabled = false;
            txtquantidade.Clear();
        }
    }
}
    
09.06.2018 / 08:41