﻿/*
 * Created by SharpDevelop.
 * User: Militho
 * Date: 03/06/2014
 * Time: 10:25 a.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using MySql.Data.MySqlClient;

namespace proyecto_final
{
	/// <summary>
	/// Description of Form1.
	/// </summary>
	public partial class Form1 : Form
	{
		private MySqlCommand myCmdQuery;
		private MySqlDataAdapter myDataAdapter;
		private BindingSource myBindingSource;
		private MySqlCommandBuilder myCommandBuilder;
		private DataSet myDataSet;
		private MySqlConnection myConnection;
		private string myStringCon;
		
		public Form1()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			this.FormBorderStyle = FormBorderStyle.FixedSingle;
			
			this.myCmdQuery= new MySqlCommand();
			this.myDataAdapter=new MySqlDataAdapter();
			this.myBindingSource=new BindingSource();
			this.myCommandBuilder =new MySqlCommandBuilder();
			this.myDataSet=new DataSet();
			this.myStringCon=
				"Server=localhost;" +
				"Database=control_usuarios;" +
				"User ID=root;" +
				"Password=Milidron;" +
				"Pooling=false;";
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		private int ejecutarComando(string sql){
			MySqlCommand myCommand = new MySqlCommand(sql,this.myConnection);
			int afectadas = myCommand.ExecuteNonQuery();
			myCommand.Dispose();
			myCommand = null;
			return afectadas;
		}
		
		public void actualizarTabla(){
			try{
				this.myDataSet.Clear();
				this.myDataAdapter.Fill(this.myDataSet,"control_usuarios");
				this.myBindingSource.DataSource=this.myDataSet;
				this.myBindingSource.DataMember="control_usuarios";
				this.myDataGrid.DataSource=this.myBindingSource;
				this.myDataGrid.Update();
				this.myDataGrid.Refresh();
			}catch(MySqlException ex){
				MessageBox.Show(ex.Message);
			}
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			Agregar_Usuario agregar=new Agregar_Usuario();
			agregar.Show();
			
			this.Hide();
		}
		
		void Button6Click(object sender, EventArgs e)
		{
			this.Close();
			
		}
		
		void Button3Click(object sender, EventArgs e)
		{
			string id = myDataGrid.CurrentRow.Cells[0].Value.ToString();
			Editar_Usuario editar= new Editar_Usuario(this,id);
			editar.Show();
			
			this.Hide();
			

		}
		
		void Button4Click(object sender, EventArgs e)
		{
			String id = myDataGrid.CurrentRow.Cells[0].Value.ToString();
			this.eliminarPorIdp(id);
			this.actualizarTabla();
			

		MessageBox.Show("Se elimino correctamente ");
		
		}
		
		private void eliminarPorIdp(string id){
			String sql ="DELETE FROM usuarios WHERE (`id`='"+id+"')";
			this.ejecutarComando(sql);
		}
		
		
		void Button1Click(object sender, EventArgs e)
		{
			this.actualizarTabla();
		}
		
		void Form1Load(object sender, EventArgs e)
		{
			this.myConnection = new MySqlConnection(this.myStringCon);
			try{
				this.myConnection.Open();
			}catch(MySqlException ex){
				MessageBox.Show(ex.Message);
				System.Environment.Exit(1);
			}

			this.myCmdQuery.CommandText="SELECT * FROM usuarios";
			this.myCmdQuery.CommandType=CommandType.Text;
			this.myCmdQuery.Connection=this.myConnection;

			this.myDataAdapter.SelectCommand=this.myCmdQuery;
			this.myCommandBuilder.DataAdapter=this.myDataAdapter;

			//Llenar el dataset
			this.myDataAdapter.Fill(this.myDataSet,"control_usuarios");
			this.myBindingSource.DataSource=this.myDataSet;
			this.myBindingSource.DataMember="control_usuarios";
			this.myDataGrid.DataSource=this.myBindingSource;
		}
	}
}
