private void buttonGuardar_Click(object sender, EventArgs e) { limpiarErrores(); DateTime Result; int parsedValue; DateTimeFormatInfo info = new DateTimeFormatInfo ( ); CultureInfo culture; culture = CultureInfo.CreateSpecificCulture ( "en-US" ); info.ShortDatePattern = "dd/MM/yyyy"; Regex expEmail = new Regex(@"^([\w.-]+)@([\w-]+)((.(\w){2,3})+)$"); if ( !(DateTime.TryParse ( textBoxBirthday.Text.Trim(), info, DateTimeStyles.None, out Result )) ) { lblErrorDateFormat.Visible = true; lblErrorBirthday.Visible = true; } else if (!int.TryParse(textBoxDocument.Text.Trim(), out parsedValue)) { lblErrorNumberValue.Visible = true; lblErrorDocument.Visible = true; } else if (listBoxHoteles.Items.Count == 0) { lbllistBoxNoItem.Visible = true; lblErrorHotel.Visible = true; } else if ((DateTime.Compare(DataBase.fechaSistema(), DateTime.Parse(textBoxBirthday.Text.Trim())))<=0) { lblErrorBirthday.Visible = true; lblBirthdayPost.Visible = true; } else if (listBoxRoles.Items.Count == 0) { lbllistBoxNoItem.Visible = true; lblErrorRole.Visible = true; } else if (!int.TryParse(textBoxBlockNumber.Text.Trim(), out parsedValue)) { lblErrorNumberValue.Visible = true; lblErrorBlockNumber.Visible = true; } else if (textBoxUserName.Text == "") { lblErrorNoField.Visible = true; lblErrorNoName.Visible = true; } else if (textBoxPW.Text == "") { lblErrorNoField.Visible = true; lblErrorPW.Visible = true; } else if (textBoxMail.Text == "") { lblErrorNoField.Visible = true; lblErrorMail.Visible = true; } else if (!expEmail.IsMatch(textBoxMail.Text)) { MessageBox.Show("Formato de mail ingresado incorrecto", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); lblErrorMail.Visible = true; } else if (textBoxBlock.Text == "") { lblErrorNoField.Visible = true; lblErrorBlock.Visible = true; } else if (textBoxBlockNumber.Text == "") { lblErrorNoField.Visible = true; lblErrorBlockNumber.Visible = true; } else if (textBoxUser.Text == "") { lblErrorNoField.Visible = true; lblErrorUser.Visible = true; } else if (textBoxSurname.Text == "") { lblErrorNoField.Visible = true; lblErrorNoSurname.Visible = true; } else if (textBoxBirthday.Text == "") { lblErrorNoField.Visible = true; lblErrorBirthday.Visible = true; } else if (textBoxDocument.Text == "") { lblErrorNoField.Visible = true; lblErrorDocument.Visible = true; } else if (textBoxDocumentType.Text == "") { lblErrorNoField.Visible = true; lblErrorDocumentType.Visible = true; } else { string usuarioExistente = string.Format("SELECT COUNT(*) FROM CAIA_UNLIMITED.Usuario WHERE usur_username = '******'", textBoxUser.Text.Trim()); SqlConnection connection = DataBase.conectarBD(); SqlCommand commUser = new SqlCommand(usuarioExistente, connection); Int32 countUsuarios = Convert.ToInt32(commUser.ExecuteScalar()); if (countUsuarios > 0) { connection.Close(); MessageBox.Show("Ya existe un nombre de usuario con el nombre indicado. Intente con otro nombre."); lblErrorUser.Visible = true; } else { RolitiesCollection rc = new RolitiesCollection(); foreach (string rol in listBoxRoles.Items) { for (int i = 0; i < this._dsRoles.Tables[0].Rows.Count; i++) { if (((string)(_dsRoles.Tables[0].Rows[i]["rol_nombre"])) == rol) { rc.Add(new Rolities { Roles = ((decimal)(this._dsRoles.Tables[0].Rows[i]["rol_codigo"])) }); } } } HotelitiesCollection hc = new HotelitiesCollection(); foreach (string hotel in listBoxHoteles.Items) { for (int i = 0; i < this._dsHoteles.Tables[0].Rows.Count; i++) { if (((string)(_dsHoteles.Tables[0].Rows[i]["Hotel"])) == hotel) { hc.Add(new Hotelities { Hoteles = ((decimal)(this._dsHoteles.Tables[0].Rows[i]["idHotel"])) }); } } } try { SqlConnection createConnection = DataBase.conectarBD(); SqlCommand insertCommand = new SqlCommand("[CAIA_UNLIMITED].sp_CrearUsuarios", createConnection); insertCommand.CommandType = CommandType.StoredProcedure; insertCommand.Parameters.AddWithValue("@username", textBoxUser.Text.Trim()); insertCommand.Parameters.AddWithValue("@password", encryptPassword(textBoxPW.Text.Trim())); insertCommand.Parameters.AddWithValue("@name", textBoxUserName.Text.Trim()); insertCommand.Parameters.AddWithValue("@apellido", textBoxSurname.Text.Trim()); insertCommand.Parameters.AddWithValue("@nacionalidad", DBNull.Value); insertCommand.Parameters.AddWithValue("@tipoDocumento", textBoxDocumentType.Text.Trim()); insertCommand.Parameters.AddWithValue("@documento", Decimal.Parse(textBoxDocument.Text.Trim())); insertCommand.Parameters.AddWithValue("@fechaNacimiento", DateTime.Parse(textBoxBirthday.Text.Trim())); if (textBoxCountry.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@pais", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@pais", textBoxCountry.Text.Trim()); } if (textBoxCity.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@ciudad", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@ciudad", textBoxCity.Text.Trim()); } insertCommand.Parameters.AddWithValue("@calle", textBoxBlock.Text.Trim()); insertCommand.Parameters.AddWithValue("@numeroCalle", textBoxBlockNumber.Text.Trim()); if (textBoxPiso.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@piso", DBNull.Value); } else { if (!int.TryParse(textBoxPiso.Text.Trim(), out parsedValue)) { lblErrorNumberValue.Visible = true; lblErrorPiso.Visible = true; return; } insertCommand.Parameters.AddWithValue("@piso", Decimal.Parse(textBoxPiso.Text.Trim())); } if (textBoxDepartamento.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@departamento", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@departamento", textBoxDepartamento.Text.Trim()); } if (textBoxTelefono.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@telefono", DBNull.Value); } else { if (!int.TryParse(textBoxTelefono.Text.Trim(), out parsedValue)) { lblErrorNumberValue.Visible = true; lblErrorTelefono.Visible = true; return; } insertCommand.Parameters.AddWithValue("@telefono", Decimal.Parse(textBoxTelefono.Text.Trim())); } insertCommand.Parameters.AddWithValue("@mail", textBoxMail.Text.Trim()); SqlParameter listRolesParam = insertCommand.Parameters.AddWithValue("@lista_Roles", rc); listRolesParam.SqlDbType = SqlDbType.Structured; listRolesParam.TypeName = "[CAIA_UNLIMITED].RolesLista"; SqlParameter listHotelesParam = insertCommand.Parameters.AddWithValue("@lista_Hoteles", hc); listHotelesParam.SqlDbType = SqlDbType.Structured; listHotelesParam.TypeName = "[CAIA_UNLIMITED].HotelesLista"; insertCommand.ExecuteNonQuery(); createConnection.Close(); string viewModificacion = string.Format("SELECT * FROM (SELECT u.usur_username as NombreDeUsuario, u.usur_habilitado as Habilitado, u.usur_nombre as Nombre, u.usur_apellido as Apellido, u.usur_documento_tipo as TipoDocumento, u.usur_documento Documento, u.usur_nacimiento as Nacimiento, u.usur_mail as Mail, d.dire_id as idDireccion, d.dire_pais as Pais, d.dire_telefono as Telefono, d.dire_dom_calle as Calle, d.dire_nro_calle as NumeroCalle, d.dire_piso Piso, d.dire_dpto as Departamento, d.dire_ciudad as Ciudad,ROW_NUMBER() OVER(PARTITION BY u.usur_username ORDER BY u.usur_username DESC) rn FROM CAIA_UNLIMITED.Usuario u JOIN CAIA_UNLIMITED.Usuario_X_Hotel uh on (u.usur_id = uh.usur_hote_idusur AND uh.usur_hote_idhote = '{0}') JOIN CAIA_UNLIMITED.Direccion d on (d.dire_id = u.dire_id OR d.dire_id IS NULL OR u.dire_id IS NULL)) a WHERE rn = 1", this.idHotel); DataSet dsViewModificacion = DataBase.realizarConsulta(viewModificacion); dataGridViewModificarUsuarios.DataSource = dsViewModificacion.Tables[0]; dataGridViewModificarUsuarios.AllowUserToAddRows = false; string viewEliminar = string.Format("SELECT * FROM (SELECT u.usur_username as NombreDeUsuario, u.usur_habilitado as Habilitado, u.usur_nombre as Nombre, u.usur_apellido as Apellido, u.usur_documento_tipo as TipoDocumento, u.usur_documento Documento, u.usur_nacimiento as Nacimiento, u.usur_mail as Mail, d.dire_id as idDireccion, d.dire_pais as Pais, d.dire_telefono as Telefono, d.dire_dom_calle as Calle, d.dire_nro_calle as NumeroCalle, d.dire_piso Piso, d.dire_dpto as Departamento, d.dire_ciudad as Ciudad,ROW_NUMBER() OVER(PARTITION BY u.usur_username ORDER BY u.usur_username DESC) rn FROM CAIA_UNLIMITED.Usuario u JOIN CAIA_UNLIMITED.Usuario_X_Hotel uh on (u.usur_id = uh.usur_hote_idusur AND uh.usur_hote_idhote = '{0}') JOIN CAIA_UNLIMITED.Direccion d on (d.dire_id = u.dire_id OR d.dire_id IS NULL OR u.dire_id IS NULL) WHERE u.usur_habilitado = 1) a WHERE rn = 1", this.idHotel); DataSet dsViewEliminar = DataBase.realizarConsulta(viewEliminar); dataGridViewEliminarUsuarios.DataSource = dsViewEliminar.Tables[0]; dataGridViewEliminarUsuarios.AllowUserToAddRows = false; MessageBox.Show("Usuario creado exitosamente!"); limpiarErrores(); limpiarListBox(); limpiarTextBox(); cargarComboBoxRol(); cargarComboBoxHotel(); } catch (Exception errorDB) { MessageBox.Show(errorDB.Message); } } } }
private void buttonDialogGuardar_Click(object sender, EventArgs e) { limpiarErrores(); DateTime Result; int parsedValue; DateTimeFormatInfo info = new DateTimeFormatInfo(); CultureInfo culture; culture = CultureInfo.CreateSpecificCulture("en-US"); info.ShortDatePattern = "dd/MM/yyyy"; Regex expEmail = new Regex(@"^([\w.-]+)@([\w-]+)((.(\w){2,3})+)$"); if (!(DateTime.TryParse(textBoxDialogBirthday.Text.Trim(), info, DateTimeStyles.None, out Result))) { lblErrorDialogDateFormat.Visible = true; lblErrorDialogBirthday.Visible = true; } else if (!int.TryParse(textBoxDialogDocument.Text.Trim(), out parsedValue)) { lblErrorDialogNumericValue.Visible = true; lblErrorDialogDocument.Visible = true; } else if (listBoxDialogHoteles.Items.Count == 0) { lblErrorDialogListBoxEmpty.Visible = true; lblErrorDialogHotel.Visible = true; } else if ((DateTime.Compare(DataBase.fechaSistema(), DateTime.Parse(textBoxDialogBirthday.Text.Trim()))) <= 0) { lblErrorDialogBirthday.Visible = true; lblErrorDialogFechaValida.Visible = true; } else if (listBoxDialogRoles.Items.Count == 0) { lblErrorDialogListBoxEmpty.Visible = true; lblErrorDialogRole.Visible = true; } else if (!int.TryParse(textBoxDialogBlockNumber.Text.Trim(), out parsedValue)) { lblErrorDialogNumericValue.Visible = true; lblErrorDialogBlockNumber.Visible = true; } else if (textBoxDialogUsername.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogUserName.Visible = true; } else if (textBoxDialogPW.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogPW.Visible = true; } else if (textBoxDialogMail.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogMail.Visible = true; } else if (!expEmail.IsMatch(textBoxDialogMail.Text)) { MessageBox.Show("Formato de mail ingresado incorrecto", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); lblErrorDialogMail.Visible = true; } else if (textBoxDialogBlock.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogBlock.Visible = true; } else if (textBoxDialogBlockNumber.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogBlockNumber.Visible = true; } else if (textBoxDialogUser.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogUser.Visible = true; } else if (textBoxDialogSurname.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogSurname.Visible = true; } else if (textBoxDialogBirthday.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogBirthday.Visible = true; } else if (textBoxDialogDocument.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogDocument.Visible = true; } else if (textBoxDialogDocumentType.Text == "") { lblErrorDialogNoValueInField.Visible = true; lblErrorDialogDocumentType.Visible = true; } else { string usuarioExistente = string.Format("SELECT COUNT(*) FROM CAIA_UNLIMITED.Usuario WHERE usur_username = '******' AND usur_username != '{1}'", textBoxDialogUser.Text.Trim(), this.nombreUsuario); SqlConnection connectionExistente = DataBase.conectarBD(); SqlCommand commUserExistente = new SqlCommand(usuarioExistente, connectionExistente); Int32 countExistente = Convert.ToInt32(commUserExistente.ExecuteScalar()); if (countExistente > 0) { connectionExistente.Close(); MessageBox.Show("Ya existe un nombre de usuario con el nombre indicado. Intente con otro nombre."); lblErrorDialogUser.Visible = true; return; } RolitiesCollection rc = new RolitiesCollection(); foreach (string rol in listBoxDialogRoles.Items) { for (int i = 0; i < this._dsRoles.Tables[0].Rows.Count; i++) { if (((string)(_dsRoles.Tables[0].Rows[i]["rol_nombre"])) == rol) { rc.Add(new Rolities { Roles = ((decimal)(this._dsRoles.Tables[0].Rows[i]["rol_codigo"])) }); } } } HotelitiesCollection hc = new HotelitiesCollection(); foreach (string hotel in listBoxDialogHoteles.Items) { for (int i = 0; i < this._dsHoteles.Tables[0].Rows.Count; i++) { if (((string)(_dsHoteles.Tables[0].Rows[i]["Hotel"])) == hotel) { hc.Add(new Hotelities { Hoteles = ((decimal)(this._dsHoteles.Tables[0].Rows[i]["idHotel"])) }); } } } try { SqlConnection createConnection = DataBase.conectarBD(); SqlCommand insertCommand = new SqlCommand("[CAIA_UNLIMITED].sp_ModificarUsuarios", createConnection); insertCommand.CommandType = CommandType.StoredProcedure; insertCommand.Parameters.AddWithValue("@idDireccion", this.direccion); insertCommand.Parameters.AddWithValue("@username", textBoxDialogUser.Text.Trim()); insertCommand.Parameters.AddWithValue("@password", encryptPassword(textBoxDialogPW.Text.Trim())); insertCommand.Parameters.AddWithValue("@name", textBoxDialogUsername.Text.Trim()); insertCommand.Parameters.AddWithValue("@apellido", textBoxDialogSurname.Text.Trim()); insertCommand.Parameters.AddWithValue("@nacionalidad", DBNull.Value); insertCommand.Parameters.AddWithValue("@tipoDocumento", textBoxDialogDocumentType.Text.Trim()); insertCommand.Parameters.AddWithValue("@documento", Decimal.Parse(textBoxDialogDocument.Text.Trim())); insertCommand.Parameters.AddWithValue("@fechaNacimiento", DateTime.Parse(textBoxDialogBirthday.Text.Trim())); if (radioButtonHabilitado.Checked) { insertCommand.Parameters.AddWithValue("@estado", true); } else { insertCommand.Parameters.AddWithValue("@estado", false); } if (textBoxDialogCountry.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@pais", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@pais", textBoxDialogCountry.Text.Trim()); } if (textBoxDialogCity.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@ciudad", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@ciudad", textBoxDialogCity.Text.Trim()); } insertCommand.Parameters.AddWithValue("@calle", textBoxDialogBlock.Text.Trim()); insertCommand.Parameters.AddWithValue("@numeroCalle", textBoxDialogBlockNumber.Text.Trim()); if (textBoxDialogPiso.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@piso", DBNull.Value); } else { if (!int.TryParse(textBoxDialogPiso.Text.Trim(), out parsedValue)) { lblErrorDialogNumericValue.Visible = true; lblErrorDialogPiso.Visible = true; return; } insertCommand.Parameters.AddWithValue("@piso", Decimal.Parse(textBoxDialogPiso.Text.Trim())); } if (textBoxDialogDepartamento.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@departamento", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@departamento", textBoxDialogDepartamento.Text.Trim()); } if (textBoxDialogTelefono.Text.Trim() == "") { insertCommand.Parameters.AddWithValue("@telefono", DBNull.Value); } else { if (!int.TryParse(textBoxDialogTelefono.Text.Trim(), out parsedValue)) { lblErrorDialogNumericValue.Visible = true; lblErrorDialogTelefono.Visible = true; return; } insertCommand.Parameters.AddWithValue("@telefono", Decimal.Parse(textBoxDialogTelefono.Text.Trim())); } insertCommand.Parameters.AddWithValue("@mail", textBoxDialogMail.Text.Trim()); SqlParameter listRolesParam = insertCommand.Parameters.AddWithValue("@lista_Roles", rc); listRolesParam.SqlDbType = SqlDbType.Structured; listRolesParam.TypeName = "[CAIA_UNLIMITED].RolesLista"; SqlParameter listHotelesParam = insertCommand.Parameters.AddWithValue("@lista_Hoteles", hc); listHotelesParam.SqlDbType = SqlDbType.Structured; listHotelesParam.TypeName = "[CAIA_UNLIMITED].HotelesLista"; insertCommand.ExecuteNonQuery(); createConnection.Close(); MessageBox.Show("Usuario modificado exitosamente!"); limpiarErrores(); limpiarListBox(); limpiarTextBox(); cargarComboBoxRol(); cargarComboBoxHotel(); if (fuiModificado) { this.Hide(); new Usuario().Show(); } else { this.Hide(); new VistaUsuario(this.miHotel, this.codigoMiRol, this.miNombreUsuario).Show(); } } catch (Exception errorDB) { MessageBox.Show(errorDB.Message); } } }