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

Asked

Viewed 27 times

-1

Hello, I have a problem, I enter the following problem: When I enter the data in the datagridview it does not "pull" from the database the information to put on the screen. BD is Mysql, Localhost and Naum has errors. Follow 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();
        }
    }
}

inserir a descrição da imagem aqui

  • when pressing a key in the quantity field, the first thing it does is clear the field?!

1 answer

0

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

Second if you have any problem in your code the connection will not be closed and is not giving Ispose on objects.

Note that I’m using "using" and eliminated "Clear" before using them (because it didn’t make any sense).

Try it this way:

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();
        }
    }
}
  • Now gave error: input string was not in correct format

  • You put a breakpoint to see where the error happens ? It’s probably in these quantity inputs, total price, where you’re giving Cast to Int. check out these details.

Browser other questions tagged

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