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 agenda_de_Contatos
{
    public partial class Form1 : Form
    {
        private MySqlConnection objCnx = new MySqlConnection();
        private MySqlCommand objCmd= new MySqlCommand();
        private MySqlDataReader objDados;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
           objCnx.ConnectionString = "Server=localhost;Database=bdcapacitacao;User=root;Pwd=neon321";
            objCnx.Open();
            }
            catch (Exception Erro)
            {
                MessageBox.Show(Erro.Message);
                
            }
 
        }

        private void btnIncluir_Click(object sender, EventArgs e)
        {
            try
            {
                string strSql = "select * from tblagenda where agdid = " + txtCodigo.Text;
                objCmd.Connection = objCnx;
                objCmd.CommandText = strSql;
                objDados = objCmd.ExecuteReader();

                if (objDados.HasRows)
                {
                    MessageBox.Show("Código existente!", "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCodigo.Focus();
                }
                else
                {
                    if (!objDados.IsClosed) { objDados.Close(); }
                    strSql = "insert into tblagenda (agdid, agdnome, agdemail, agdtelefone, agdcpf) values(";
                    strSql += txtCodigo.Text + ",";
                    strSql += "'" + txtNome.Text + "',";
                    strSql += "'" + txtEmail.Text + "',";
                    strSql += "'" + txtTelefone.Text + "',";
                    strSql += "'" + txtCpf.Text + "')";
                    
                    objCmd.Connection = objCnx;
                    objCmd.CommandText = strSql;
                    objCmd.ExecuteNonQuery();

                    MessageBox.Show("Registro incluído com sucesso!", "ADO.NET",MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                if(objDados.IsClosed) { objDados.Close(); }
            }
            catch (Exception Erro)
            {

                MessageBox.Show("Erro ==> " + Erro.Message, "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void btnAlterar_Click(object sender, EventArgs e)
        {
            try
            {
                string strSql = "select * from tblagenda where agdid = " + txtCodigo.Text;
                objCmd.Connection = objCnx;
                objCmd.CommandText = strSql;
                objDados = objCmd.ExecuteReader();

                if (!objDados.HasRows)
                {
                    MessageBox.Show("Código Inexistente!", "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCodigo.Focus();
                }
                else
                {
                    if (!objDados.IsClosed) { objDados.Close(); }
                    strSql = "update tblagenda set ";
                    strSql += " agdnome ='" + txtNome.Text + "',";
                    strSql += " agdemail ='" + txtEmail.Text + "',";
                    strSql += " agdtelefone ='" + txtTelefone.Text + "',";
                    strSql += " agdcpf ='" + txtCpf.Text + "' ";
                    strSql += "where agdid = " + txtCodigo.Text;

                    objCmd.Connection = objCnx;
                    objCmd.CommandText = strSql;
                    objCmd.ExecuteNonQuery();

                    MessageBox.Show("Registro Alterado com sucesso!", "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                if (objDados.IsClosed) { objDados.Close(); }
            }
            catch (Exception Erro)
            {
                MessageBox.Show("Erro ==> " + Erro.Message, "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void btnListaDeDados_Click(object sender, EventArgs e)
        {
            frmConsultarLista objTela = new frmConsultarLista();
            objTela.ShowDialog();
        }

        private void btnSair_Click(object sender, EventArgs e)
        {
            objCnx.Close();
            Application.Exit();
        }

        private void btnConsultar_Click(object sender, EventArgs e)
        {
            try
            {
                string strSql = "select * from tblagenda where agdid = " + txtCodigo.Text;
                objCmd.Connection = objCnx;
                objCmd.CommandText = strSql;
                objDados = objCmd.ExecuteReader();

                if (!objDados.HasRows)
                {
                    MessageBox.Show("Código inexistente!", "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCodigo.Focus();
                }
                else
                {
                    objDados.Read();
                    txtNome.Text = objDados["agdnome"].ToString();
                    txtEmail.Text = objDados["agdemail"].ToString();
                    txtTelefone.Text = objDados["agdtelefone"].ToString();
                    txtCpf.Text = objDados["agdcpf"].ToString();



                }
                if (!objDados.IsClosed){objDados.Close();
                }

            }
            catch (Exception Erro)
            {
                MessageBox.Show("Erro ==> " + Erro.Message, "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void btnExcluir_Click(object sender, EventArgs e)
        {
            try
            {
                string strSql = "select * from tblagenda where agdid = " + txtCodigo.Text;
                objCmd.Connection = objCnx;
                objCmd.CommandText = strSql;
                objDados = objCmd.ExecuteReader();

                if (!objDados.HasRows)
                {
                    MessageBox.Show("Código inexistente!", "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCodigo.Focus();
                }
                else
                {
                    if (!objDados.IsClosed) { objDados.Close(); }
                    if (MessageBox.Show("Deseja excluir este registro?","ADO.NET",MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)== DialogResult.Yes)
                    {
                        strSql = "delete from tblagenda where agdid = " + txtCodigo.Text;
                        objCmd.Connection = objCnx;
                        objCmd.CommandText = strSql;
                        objCmd.ExecuteNonQuery();
                        MessageBox.Show("Registro eliminado com sucesso!", "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                }
                if (!objDados.IsClosed) { objDados.Close(); }
             }
            catch (Exception Erro)
            {

                MessageBox.Show("Erro ==> " + Erro.Message, "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnLimpar_Click(object sender, EventArgs e)
        {
            txtCodigo.Text = string.Empty;
            txtNome.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtTelefone.Text = string.Empty;
            txtCpf.Text = string.Empty;
            txtCodigo.Focus();



        }
    }
}
