Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 System.DATA.SqlClient;
- namespace SiCoMat
- {
- public partial class VentanaCrearProyecto : Form
- {
- VentanaSicomat padre;
- SqlConnection conexion;
- SqlCommand comando;
- SqlDataReader lector;
- String connectionString = Program.getConnectionString();
- public VentanaCrearProyecto(VentanaSicomat ventanaSicomat)
- {
- InitializeComponent();
- padre = ventanaSicomat;
- //dateTimePickerFechaInicioProyecto.CustomFormat = "yyyy-MM-dd";
- dateTimePickerFechaInicioProyecto.Format = DateTimePickerFormat.Custom;
- dateTimePickerFechaInicioProyecto.CustomFormat = "yyyy-MM-dd";
- dateTimePickerFechaFinalizacionProyecto.Format = DateTimePickerFormat.Custom;
- dateTimePickerFechaFinalizacionProyecto.CustomFormat = "yyyy-MM-dd";
- }
- private void VentanaCrearProyecto_Load(object sender, EventArgs e)
- {
- }
- public bool existeProyecto()
- {
- bool respuesta = FALSE;
- try
- {
- conexion = NEW SqlConnection(Program.getConnectionString());
- conexion.OPEN();
- String consulta = string.Format("select * from proyectos", textBoxNombreProyecto.Text);
- comando = NEW SqlCommand(consulta, conexion);
- lector = comando.ExecuteReader();
- lector.READ();
- do
- {
- String nombreProyecto = lector[2].ToString();
- IF (nombreProyecto == textBoxNombreProyecto.Text)
- {
- respuesta = TRUE;
- break;
- }
- } while (lector.READ());
- lector.Close();
- conexion.Close();
- }
- catch (SqlException e)
- {
- MessageBox.SHOW("No se pudo realizar la consulta. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- RETURN respuesta;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- // dateTimePickerFechaInicioProyecto
- //String fechaInicio = dateTimePickerFechaInicioProyecto.VALUE.DATE.ToString("yyyy-MM-dd");
- //String fechaFinalizacion = dateTimePickerFechaFinalizacionProyecto.VALUE.DATE.ToString("yyyy-MM-dd");
- //Console.WriteLine(dateTimePickerFechaInicioProyecto.VALUE.DATE.ToString("yyyy-MM-dd"));
- bool existeProyecto = this.existeProyecto();
- bool existeAsada = this.existeAsada();
- IF (textBoxNombreProyecto.Text == "")
- {
- MessageBox.SHOW("Agregue un nombre al proyecto. ", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- ELSE IF (textBoxDescripcionProyecto.Text == "")
- {
- MessageBox.SHOW("Agregue la descripcion del proyecto. ", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- ELSE IF (dateTimePickerFechaInicioProyecto.VALUE > dateTimePickerFechaFinalizacionProyecto.VALUE)
- {
- MessageBox.SHOW("La fecha de inicio debe ser anterior a la fecha de finalizacion. ", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- ELSE IF (!existeAsada)
- {
- MessageBox.SHOW("No existe la asada especificada. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- ELSE IF (existeProyecto)
- {
- MessageBox.SHOW("Ya existe un proyecto con este nombre. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- ELSE
- {
- String fechaInicio = dateTimePickerFechaInicioProyecto.VALUE.DATE.ToString("yyyy-MM-dd");
- String fechaFinalizacion = dateTimePickerFechaFinalizacionProyecto.VALUE.DATE.ToString("yyyy-MM-dd");
- insertarProyecto(textBoxNombreProyecto.Text, textBoxAsada.Text, textBoxDescripcionProyecto.Text, fechaInicio, fechaFinalizacion);
- this.Close();
- this.padre.SHOW();
- }
- }
- public void insertarProyecto(string nombreProyecto, string idAsada, string descripcionProyecto, string fechaInicioProyecto, string fechaFinalizacionProyecto)
- {
- SqlConnection con = NEW SqlConnection(Program.getConnectionString());
- SqlCommand cmd; // = NEW SqlCommand();
- //string queryDificil = "insert into sedes (nombre, idDistrito, direccionExacta) values('"+nombre+"','"+distrito+"','"+direccion+"')";
- string queryFacil = String.Format(
- "EXEC insertarProyecto @nombre = '{0}' ,@idAsada = {1} , @descripcion = '{2}', @fechaInicio='{3}',@fechaFinalizacion = '{4}'", // El punto y coma al final no es necesario
- nombreProyecto, idAsada, descripcionProyecto, fechaInicioProyecto, fechaFinalizacionProyecto);
- try
- {
- con.OPEN();
- cmd = NEW SqlCommand(queryFacil, con); // 3 formas de recibirlo, usaremos la tercera
- cmd.ExecuteNonQuery();
- MessageBox.SHOW("Proyecto agregado correctamente", "Insercion exitosa", MessageBoxButtons.OK, MessageBoxIcon.Information);
- con.Close();
- //this.Close();
- }
- catch (Exception e)
- {
- MessageBox.SHOW("No se ha podido agregar el proyecto:\n" + e.Message, "Error de insercion", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- public bool existeAsada()
- {
- bool respuesta = FALSE;
- try
- {
- conexion = NEW SqlConnection(Program.getConnectionString());
- conexion.OPEN();
- String consulta = string.Format("select * from asadas", textBoxAsada.Text);
- comando = NEW SqlCommand(consulta, conexion);
- lector = comando.ExecuteReader();
- lector.READ();
- do
- {
- String idAsada = lector[0].ToString();
- IF (idAsada == textBoxAsada.Text)
- {
- respuesta = TRUE;
- break;
- }
- } while (lector.READ());
- lector.Close();
- conexion.Close();
- }
- catch (SqlException e)
- {
- MessageBox.SHOW("No se pudo realizar la consulta. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- RETURN respuesta;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement