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 frmConsultarLista : Form
    {
        private MySqlConnection objCnx = new MySqlConnection();
        private MySqlCommand objCmd = new MySqlCommand();
        private MySqlDataReader objDados;

        public frmConsultarLista()
        {
            InitializeComponent();
        }

        private void frmConsultarLista_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 btnConsultar_Click(object sender, EventArgs e)
        {
            try
            {
                string strSql;
                strSql = "select * from tblagenda order by agdnome";
                objCmd.CommandText = strSql;
                objCmd.Connection = objCnx;
                objDados = objCmd.ExecuteReader();
                if (!objDados.HasRows)
                {
                    MessageBox.Show("Código Inexistente!", "Consultar lista", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    while(objDados.Read())
                    {
                        dgvDados.Rows.Add(Properties.Resources.pesquisar_24px, objDados["agdid"].ToString(), objDados["agdnome"].ToString(),
 objDados["agdtelefone"].ToString(), objDados["agdcpf"].ToString());
                    }
                }
                objDados.Close();
            }
            catch (Exception Erro)
            {
                MessageBox.Show(Erro.Message);

            }
        }

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