//Método OnClick para realizar el regreso del libro protected void Regreso(Object sender, EventArgs e) { Prestamo P = new Prestamo(); if (DisponibilidadLibro() && ExisteUsuario()) { if (ExistePrestamo()) { P.Usuario = txtUsuario.Text.Trim(); P.Id_libro = txtIdLibro.Text.Trim(); try { SqlConnection cnx = new SqlConnection(CadenaCnx); //Veemos si la conexión está cerrada if (cnx.State == ConnectionState.Closed) { //Abrimos la conexión cnx.Open(); } //Creamos la consulta con los campos correspondientes, y con parámetros para facilidad y seguridad SqlCommand stm = new SqlCommand("DELETE FROM tbl_prestamos WHERE prst_usuario = @prst_usuario AND prst_libro_id=@prst_libro_id ", cnx); //Pasamos los parámetros con los datos de la vista stm.Parameters.AddWithValue("@prst_usuario", P.Usuario.Trim()); stm.Parameters.AddWithValue("@prst_libro_id", P.Id_libro.Trim()); //Ejecutamos la consulta int RES = stm.ExecuteNonQuery(); if (RES > 0) { //Reducimos el stock disponible en 1 stm = new SqlCommand("UPDATE tbl_libros SET libro_stock_actual = libro_stock_actual+1 WHERE libro_id = @libro_id", cnx); stm.Parameters.AddWithValue("@libro_id", P.Id_libro.Trim()); //Ejecutamos la consulta stm.ExecuteNonQuery(); } //Cerramos conexión cnx.Close(); ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaEcho('Libro regresado, préstamo completado')", true); limpiarCampos(); //Actualizamos la tablita GridPrestamos.DataBind(); } catch (Exception ex) { ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaFallo('" + ex.Message + "')", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaInfo('No hay un préstamo para este usuario de este libro')", true); return; } } else { ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaFallo('No se encuentra ningún registro coincidente')", true); } }
protected void Page_Load(object sender, EventArgs e) { //Checamos si el usuario esta logeado con los permisos necesarios if (Session["username"].ToString() == "" || Session["rol"].ToString() != "admin") { //Si no esta logeado lo redirigimos al login Response.Redirect("adminLogin.aspx"); } //Cargamos los datos en la carga de la página GridPrestamos.DataBind(); }
//Método OnClick para realizar el préstamo del libro protected void Prestamo(Object sender, EventArgs e) { if (ExistePrestamo()) { ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaInfo('Este usuario ya tiene un préstamo con este libro')", true); //Limpiamos para obligar buscar otro libro txtIdLibro.Text = ""; txtLibro.Text = ""; return; } else { if (CuentaActiva()) { ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaFallo('La cuenta del usuario no esta activa')", true); return; } String correo; String idUsuario = txtUsuario.Text.Trim(); String idLibro = txtIdLibro.Text.Trim(); String NombreUsuario = txtNombre.Text.Trim(); String Libro = txtLibro.Text.Trim(); DateTime FechaInicio = Convert.ToDateTime(txtPrestamo.Text); DateTime FechaEntrega = Convert.ToDateTime(txtEntrega.Text); //Vemos si la fecha de inicio es mayor a la del prestamos if (FechaEntrega <= FechaInicio) { ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaFallo('La fecha de entrega no puede ser menor a la de inicio')", true); return; } try { SqlConnection cnx = new SqlConnection(CadenaCnx); //Veemos si la conexión está cerrada if (cnx.State == ConnectionState.Closed) { //Abrimos la conexión cnx.Open(); } //Creamos la consulta con los campos correspondientes, y con parámetros para facilidad y seguridad SqlCommand stm = new SqlCommand("INSERT INTO tbl_prestamos(prst_usuario, prst_usuario_nombre, prst_libro_id, prst_libro_nombre, prst_fecha_inicio, prst_fecha_fin) " + "VALUES(@prst_usuario, @prst_usuario_nombre, @prst_libro_id, @prst_libro_nombre, @prst_fecha_inicio, @prst_fecha_fin) ", cnx); //Pasamos los parámetros con los datos de la vista stm.Parameters.AddWithValue("@prst_usuario", idUsuario.Trim()); stm.Parameters.AddWithValue("@prst_usuario_nombre", NombreUsuario.Trim()); stm.Parameters.AddWithValue("@prst_libro_id", idLibro.Trim()); stm.Parameters.AddWithValue("@prst_libro_nombre", Libro.Trim()); stm.Parameters.AddWithValue("@prst_fecha_inicio", FechaInicio); stm.Parameters.AddWithValue("@prst_fecha_fin", FechaEntrega); //Ejecutamos la consulta stm.ExecuteNonQuery(); //Reducimos el stock disponible en 1 stm = new SqlCommand("UPDATE tbl_libros SET libro_stock_actual = libro_stock_actual-1 WHERE libro_id = @libro_id", cnx); stm.Parameters.AddWithValue("@libro_id", idLibro.Trim()); //Ejecutamos la consulta stm.ExecuteNonQuery(); //Cerramos conexión stm = new SqlCommand("SELECT usr_correo FROM tbl_usuario WHERE usr_user = @usr_user", cnx); stm.Parameters.AddWithValue("@usr_user", idUsuario.Trim()); SqlDataAdapter da = new SqlDataAdapter(stm); //Ejecutamos la consulta, con DataAdapter para ejecutar más de una DataTable Tabla = new DataTable(); //Creamos tabla para almacenar los resultados da.Fill(Tabla); correo = Tabla.Rows[0]["usr_correo"].ToString(); cnx.Close(); ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaEcho('Préstamo registrado')", true); System.Diagnostics.Debug.WriteLine(correo); string body = this.EnviarEmail(NombreUsuario, Libro, FechaInicio, FechaEntrega); System.Diagnostics.Debug.WriteLine(body); this.SendHtmlFormattedEmail(correo.Trim(), "Nuevo préstamo!", body); limpiarCampos(); //Actualizamos la tablita GridPrestamos.DataBind(); } catch (SqlException ex) { ClientScript.RegisterStartupScript(this.GetType(), "Registrado", "alertaFallo('" + ex.Message + "')", true); } catch (SmtpException Ex) { Response.Write("<script>alert('" + Ex.Message + "');</script>"); } } }