Advertisement
gmendezm

Ventana de Crear Proyectos SiCoMat

May 30th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 7.05 KB | None | 0 0
  1. USING System;
  2. USING System.Collections.Generic;
  3. USING System.ComponentModel;
  4. USING System.DATA;
  5. USING System.Drawing;
  6. USING System.Linq;
  7. USING System.Text;
  8. USING System.Threading.Tasks;
  9. USING System.Windows.Forms;
  10. USING System.DATA.SqlClient;
  11.  
  12. namespace SiCoMat
  13. {
  14.     public partial class VentanaCrearProyecto : Form
  15.     {
  16.         VentanaSicomat padre;
  17.  
  18.         SqlConnection conexion;
  19.         SqlCommand comando;
  20.         SqlDataReader lector;
  21.         String connectionString = Program.getConnectionString();
  22.  
  23.         public VentanaCrearProyecto(VentanaSicomat ventanaSicomat)
  24.         {
  25.             InitializeComponent();
  26.             padre = ventanaSicomat;
  27.             //dateTimePickerFechaInicioProyecto.CustomFormat = "yyyy-MM-dd";
  28.  
  29.             dateTimePickerFechaInicioProyecto.Format = DateTimePickerFormat.Custom;
  30.             dateTimePickerFechaInicioProyecto.CustomFormat = "yyyy-MM-dd";
  31.  
  32.             dateTimePickerFechaFinalizacionProyecto.Format = DateTimePickerFormat.Custom;
  33.             dateTimePickerFechaFinalizacionProyecto.CustomFormat = "yyyy-MM-dd";
  34.            
  35.         }
  36.  
  37.         private void VentanaCrearProyecto_Load(object sender, EventArgs e)
  38.         {
  39.  
  40.         }
  41.  
  42.  
  43.         public bool existeProyecto()
  44.         {
  45.             bool respuesta = FALSE;
  46.             try
  47.             {
  48.                 conexion = NEW SqlConnection(Program.getConnectionString());
  49.                 conexion.OPEN();
  50.  
  51.                 String consulta = string.Format("select * from proyectos", textBoxNombreProyecto.Text);
  52.                 comando = NEW SqlCommand(consulta, conexion);
  53.                 lector = comando.ExecuteReader();
  54.                 lector.READ();
  55.  
  56.                 do
  57.                 {
  58.                     String nombreProyecto = lector[2].ToString();
  59.  
  60.                     IF (nombreProyecto == textBoxNombreProyecto.Text)
  61.                     {
  62.                         respuesta = TRUE;
  63.                         break;
  64.                     }
  65.                 } while (lector.READ());
  66.  
  67.                 lector.Close();
  68.                 conexion.Close();
  69.             }
  70.             catch (SqlException e)
  71.             {
  72.                 MessageBox.SHOW("No se pudo realizar la consulta. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  73.             }
  74.             RETURN respuesta;
  75.         }
  76.  
  77.         private void button1_Click(object sender, EventArgs e)
  78.         {
  79.             // dateTimePickerFechaInicioProyecto
  80.             //String fechaInicio = dateTimePickerFechaInicioProyecto.VALUE.DATE.ToString("yyyy-MM-dd");
  81.             //String fechaFinalizacion = dateTimePickerFechaFinalizacionProyecto.VALUE.DATE.ToString("yyyy-MM-dd");
  82.             //Console.WriteLine(dateTimePickerFechaInicioProyecto.VALUE.DATE.ToString("yyyy-MM-dd"));
  83.             bool existeProyecto = this.existeProyecto();
  84.             bool existeAsada = this.existeAsada();
  85.  
  86.             IF (textBoxNombreProyecto.Text == "")
  87.             {
  88.                 MessageBox.SHOW("Agregue un nombre al proyecto. ", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  89.             }
  90.             ELSE IF (textBoxDescripcionProyecto.Text == "")
  91.             {
  92.                 MessageBox.SHOW("Agregue la descripcion del proyecto. ", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  93.             }
  94.             ELSE IF (dateTimePickerFechaInicioProyecto.VALUE > dateTimePickerFechaFinalizacionProyecto.VALUE)
  95.             {
  96.                 MessageBox.SHOW("La fecha de inicio debe ser anterior a la fecha de finalizacion. ", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  97.             }
  98.             ELSE IF (!existeAsada)
  99.             {
  100.                 MessageBox.SHOW("No existe la asada especificada. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  101.             }
  102.             ELSE IF (existeProyecto)
  103.             {
  104.                 MessageBox.SHOW("Ya existe un proyecto con este nombre. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  105.             }
  106.             ELSE
  107.             {
  108.                 String fechaInicio = dateTimePickerFechaInicioProyecto.VALUE.DATE.ToString("yyyy-MM-dd");
  109.                 String fechaFinalizacion = dateTimePickerFechaFinalizacionProyecto.VALUE.DATE.ToString("yyyy-MM-dd");
  110.                 insertarProyecto(textBoxNombreProyecto.Text, textBoxAsada.Text, textBoxDescripcionProyecto.Text, fechaInicio, fechaFinalizacion);
  111.                 this.Close();
  112.                 this.padre.SHOW();
  113.             }
  114.         }
  115.  
  116.         public void insertarProyecto(string nombreProyecto, string idAsada, string descripcionProyecto, string fechaInicioProyecto, string fechaFinalizacionProyecto)
  117.         {
  118.             SqlConnection con = NEW SqlConnection(Program.getConnectionString());
  119.  
  120.             SqlCommand cmd; // = NEW SqlCommand();
  121.             //string queryDificil = "insert into sedes (nombre, idDistrito, direccionExacta) values('"+nombre+"','"+distrito+"','"+direccion+"')";
  122.             string queryFacil = String.Format(
  123.                                     "EXEC insertarProyecto @nombre = '{0}' ,@idAsada = {1} , @descripcion = '{2}', @fechaInicio='{3}',@fechaFinalizacion = '{4}'", // El punto y coma al final no es necesario
  124.                                     nombreProyecto, idAsada, descripcionProyecto, fechaInicioProyecto, fechaFinalizacionProyecto);
  125.             try
  126.             {
  127.                 con.OPEN();
  128.  
  129.                 cmd = NEW SqlCommand(queryFacil, con); // 3 formas de recibirlo, usaremos la tercera
  130.                 cmd.ExecuteNonQuery();
  131.                 MessageBox.SHOW("Proyecto agregado correctamente", "Insercion exitosa", MessageBoxButtons.OK, MessageBoxIcon.Information);
  132.                 con.Close();
  133.                 //this.Close();
  134.             }
  135.             catch (Exception e)
  136.             {
  137.                 MessageBox.SHOW("No se ha podido agregar el proyecto:\n" + e.Message, "Error de insercion", MessageBoxButtons.OK, MessageBoxIcon.Error);
  138.             }
  139.         }
  140.  
  141.  
  142.         public bool existeAsada()
  143.         {
  144.             bool respuesta = FALSE;
  145.             try
  146.             {
  147.                 conexion = NEW SqlConnection(Program.getConnectionString());
  148.                 conexion.OPEN();
  149.  
  150.                 String consulta = string.Format("select * from asadas", textBoxAsada.Text);
  151.                 comando = NEW SqlCommand(consulta, conexion);
  152.                 lector = comando.ExecuteReader();
  153.                 lector.READ();
  154.  
  155.                 do
  156.                 {
  157.                     String idAsada = lector[0].ToString();
  158.  
  159.                     IF (idAsada == textBoxAsada.Text)
  160.                     {
  161.                         respuesta = TRUE;
  162.                         break;
  163.                     }
  164.                 } while (lector.READ());
  165.  
  166.                 lector.Close();
  167.                 conexion.Close();
  168.             }
  169.             catch (SqlException e)
  170.             {
  171.                 MessageBox.SHOW("No se pudo realizar la consulta. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  172.             }
  173.             RETURN respuesta;
  174.         }
  175.  
  176.  
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement