I have a problem when I give my button Save or Insert my form, since it does not save me or present me with any error, I do not know what to do

0

This in the Order that I'm giving to my save button, so that I save the data in my database called Patient. I can not find which is the reason, when I click on the button it does not do anything to me, it's as if I did not receive the order.

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 Consultorio_Clinico
{
    public partial class Pacientes : Form
    {
        public Pacientes()
        {
            InitializeComponent();
        }
        MySqlConnection conectarp = new MySqlConnection("server=localHost;DataBase=xxxx;Uid=*****;pwd=****;");
        DataSet ds;
        MySqlDataReader ddd;
        MySqlCommand comandoX;

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Paciente_Load(object sender, EventArgs e)
        {
            PopulateX();
        }

        public void PopulateX()
        {
            string selecionado = "SELECT * FROM paciente ";
            DataTable table = new DataTable();
            MySqlDataAdapter mostrar = new MySqlDataAdapter(selecionado, conectarp);
            mostrar.Fill(table);
            dtgpaciente.DataSource = table;
            }

        private void btnguardar_Click(object sender, EventArgs e)
        {  
            try
            {
                string insertarX = "insert into paciente(id_pacient,nom_pacient,ed_pacient,tel_pacient,dir_pacient,ciud_pacient,sex_pacient) values ('" + txtidpacient.Text + "','" + txtnompacient.Text + "','" + txtedapacient.Text + "','" + txttelpacient.Text + "','" + txtdirpacient.Text + "','" + txtciudpacient.Text + "','" + txtsexopacient.Text + "')";
                EjecutarMyConsultaP(insertarX);
                PopulateX();


                MessageBox.Show("Registro guardado con exito");
            }
            catch (Exception error)
            {
                MessageBox.Show("Error.." + error.Message);
                conectarp.Close();
                txtidpacient.Clear();
                txtnompacient.Clear();
                txtedapacient.Clear();
                txttelpacient.Clear();
                txtdirpacient.Clear();
                txtciudpacient.Clear();
                txtsexopacient.Clear();

            }

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            {
                this.Close();
            }
        }

        private void dtgpaciente_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dtgpaciente_MouseClick(object sender, MouseEventArgs e)
        {
            txtidpacient.Text = dtgpaciente.CurrentRow.Cells[0].Value.ToString();
            txtnompacient.Text = dtgpaciente.CurrentRow.Cells[1].Value.ToString();
            txtedapacient.Text = dtgpaciente.CurrentRow.Cells[2].Value.ToString();
            txttelpacient.Text = dtgpaciente.CurrentRow.Cells[3].Value.ToString();
            txtdirpacient.Text = dtgpaciente.CurrentRow.Cells[4].Value.ToString();
            txtciudpacient.Text = dtgpaciente.CurrentRow.Cells[5].Value.ToString();
            txtsexopacient.Text = dtgpaciente.CurrentRow.Cells[6].Value.ToString();
        }

        public void Abrirconetion()
        {
            if (conectarp.State == ConnectionState.Closed)
            {
                conectarp.Open();
            }
        }

        public void Cerrarconetion()
        {
            if (conectarp.State == ConnectionState.Open)
            {
                conectarp.Close();
            }
        }
        public void EjecutarMyConsultaP(string ConsultaP)
        {
            try
            {
                Abrirconetion();
                comandoX = new MySqlCommand(ConsultaP,conectarp);
            }
            catch (Exception executeP)
            {
                MessageBox.Show(executeP.Message);

                if (comandoX.ExecuteNonQuery() == 1)
                { 
                MessageBox.Show ("Consulta Ejecutada");
                }
                else
                {

                    MessageBox.Show("Consulta No Ejecutada");
                }
            }
            finally
            {
                Cerrarconetion();
            }
        }

        private void btnacertar_Click(object sender, EventArgs e)
        {
            string inserQuery = "INSERT INTO paciente (id_pacient,nom_pacient,ed_pacient,tel_pacient,dir_pacient,ciud_pacient,sex_pacien VALUES ('" + txtidpacient + "','" + txtnompacient.Text + "','" + txtedapacient.Text + "','" + txtedapacient.Text + "','" + txttelpacient.Text + "','" + txtdirpacient.Text + "','" + txtciudpacient.Text + "','" + txtsexopacient.Text + "')";
            EjecutarMyConsultaP(inserQuery);
        }
    }
}
    
asked by Nataliel 17.08.2017 в 17:16
source

1 answer

5

Seeing the code, your MySql command execution is within the catch so it will not run until try generates an error, which I do not see that will ever happen, so we move the block ExecuteNonQuery to the% share% co where you must execute the query:

public void EjecutarMyConsultaP(string ConsultaP)
{
    try //Se ejecuta primero
    {
        Abrirconetion();
        comandoX = new MySqlCommand(ConsultaP,conectarp);
        if (comandoX.ExecuteNonQuery() == 1)
         { 
             MessageBox.Show ("Consulta Ejecutada");
         }
        else
        {
            MessageBox.Show("Consulta No Ejecutada");
         }
    }
    catch (Exception executeP) //Solo si ocurre un error en la ejecucion caemos en este bloque de codigo
    {
        MessageBox.Show(executeP.Message);   
    }
    finally //Despues de que el try o el catch finalizan su ejecucion se ejecuta el finally
    {
        Cerrarconetion();
    }
}
    
answered by 17.08.2017 в 17:36