/// <summary> /// Loads a movie record to edit/update/remove. /// </summary> /// <param name="item"></param> private void PoblarCamposPelicula(DetallePeliculaDto item) { if (log.IsDebugEnabled) { log.Debug("PoblarCamposPelicula Starts"); } nombre.Text = item.nombrePelicula; url.Text = item.urlArticuloEc; sinopsis.Text = item.sinopsis; listGenero.SelectedValue = item.idGeneroPelicula.ToString(); if (item.imagenCartelera != "") { Uri u = Request.Url; string imageUrl = u.Scheme + "://" + u.Authority + "/" + Settings.ImageFolder + "/" + item.imagenCartelera; imgPoster.ImageUrl = imageUrl.Replace(@"\", "/"); imgPoster.Width = 221; imgPoster.Height = 309; imgPoster.Visible = true; } else { imgPoster.Visible = false; } if (item.enCartelera.Equals("S")) { checkEnCartelera.Checked = true; } else { checkEnCartelera.Checked = false; } if (log.IsDebugEnabled) { log.Debug("PoblarCamposPelicula Ends"); } }
/// <summary> /// Creates a new record in DB. /// </summary> /// <param name="pelicula">Movie details</param> /// <param name="operacion">Operation to accomplish: 1:create, 2:update</param> /// <returns></returns> public int createPelicula(DetallePeliculaDto pelicula, int operacion) { if (log.IsDebugEnabled) { log.Debug("createPelicula Starts"); } HandleDatabase hdb = null; SqlTransaction transaction = null; SqlDataReader rdr = null; int rslt = 0; try { hdb = new HandleDatabase(Settings.Connection); hdb.Open(); List<SqlParameter> paramList = new List<SqlParameter>() { new SqlParameter() {ParameterName = "@OPERACION", Value = operacion, SqlDbType = SqlDbType.Int}, new SqlParameter() { ParameterName = "@PELICULA", Value = pelicula.idPelicula, SqlDbType = SqlDbType.Int}, new SqlParameter() { ParameterName = "@NOMBRE", Value = pelicula.nombrePelicula, SqlDbType = SqlDbType.VarChar}, new SqlParameter() { ParameterName = "@USUARIO", Value = pelicula.idUsuarioCreador, SqlDbType = SqlDbType.Int}, new SqlParameter() { ParameterName = "@GENERO", Value = pelicula.idGeneroPelicula, SqlDbType = SqlDbType.Int}, new SqlParameter() { ParameterName = "@SINOPSIS", Value = pelicula.sinopsis, SqlDbType = SqlDbType.VarChar}, new SqlParameter() { ParameterName = "@IMAGEN", Value = pelicula.imagenCartelera, SqlDbType = SqlDbType.VarChar}, new SqlParameter() { ParameterName = "@URL", Value = pelicula.urlArticuloEc, SqlDbType = SqlDbType.VarChar}, new SqlParameter() { ParameterName = "@ACTIVO", Value = pelicula.enCartelera, SqlDbType = SqlDbType.VarChar} }; String sql = "sp_crearActualizarPelicula @OPERACION,@PELICULA,@NOMBRE,@USUARIO,@GENERO,@SINOPSIS,@IMAGEN,@URL,@ACTIVO"; var i = 1; if (log.IsDebugEnabled) { log.Debug("SQL=[" + sql + "]"); paramList.ForEach(p => { var paramValues = "ParameterName=[" + p.ParameterName + "], Value=[" + p.Value + "], SqlDbType=[" + p.SqlDbType + "]"; log.Debug("Parameter " + i++ + " val=[" + paramValues + "]"); }); } transaction = hdb.BeginTransaction("crearPelicula"); rdr = hdb.ExecSelectSQLStmtAsReader(transaction, sql, paramList.ToArray()); rslt = 1; } catch (Exception ex) { if (log.IsFatalEnabled) { log.Fatal("Exception occurred " + ex.Message); log.Fatal("Exception trace=[" + ex.StackTrace + "]"); log.Fatal("Result sets to -1"); } rslt = -1; } finally { try { if (rdr != null) { rdr.Close(); } if (transaction != null) { transaction.Commit(); } if (hdb != null) { hdb.Close(); } } catch (Exception e) { if (log.IsFatalEnabled) { log.Fatal("Exception occurred " + e.Message); log.Fatal("Exception trace=[" + e.StackTrace + "]"); log.Fatal("Result sets to -1"); } rslt = -1; } } if (log.IsDebugEnabled) { log.Debug("rslt=[" + rslt + "]"); log.Debug("createPelicula Ends"); } return rslt; }
/// <summary> /// Operation to create or update a record in database. /// </summary> /// <param name="operacion">1: create, 2: update</param> /// <returns>0 if successful</returns> protected int CrearPelicula(int operacion) { if (log.IsDebugEnabled) { log.Debug("CrearPelicula Starts"); log.Debug("Selected operation [" + operacion + "]"); } Pelicula peliculaDao = new Pelicula(); DetallePeliculaDto datosPelicula = new DetallePeliculaDto(); if (operacion == 1) { datosPelicula.fechaCreacionPelicula = DateTime.Now.MapToLocalTimeColombia(); } if (operacion == 2) { if (log.IsDebugEnabled) { log.Debug("id to update [" + grdInfo.DataKeys[grdInfo.SelectedIndex].Value + "]"); } datosPelicula = peliculaDao.getPelicula(Convert.ToInt32(grdInfo.DataKeys[grdInfo.SelectedIndex].Value)); } // The following code has no effect if you use Updatepanel. // to upload an image it must be sent first to server and then somehow referenced for inserting/updating record. // Left here for compatibility. if (imgUpload.HasFile) { string imgFolder = Settings.ImageFolderSave; string imgName = imgFolder + @"\" + imgUpload.FileName; imgUpload.SaveAs(imgName); datosPelicula.imagenCartelera = imgUpload.FileName; } else { if (!String.IsNullOrEmpty(posterImageName.Value)) { datosPelicula.imagenCartelera = posterImageName.Value; } } datosPelicula.nombrePelicula = nombre.Text; datosPelicula.idUsuarioCreador = 1; datosPelicula.idGeneroPelicula = Convert.ToInt32(listGenero.SelectedValue); datosPelicula.sinopsis = sinopsis.Text; datosPelicula.urlArticuloEc = url.Text; if (checkEnCartelera.Checked) { datosPelicula.enCartelera = "S"; } else { datosPelicula.enCartelera = "N"; } if (log.IsDebugEnabled) { log.Debug("Record data [" + datosPelicula.ToString() + "]"); } int resultado = peliculaDao.createPelicula(datosPelicula, operacion); if (resultado == -1) { if (log.IsDebugEnabled) { log.Debug("Record not created"); } registerToastrMsg(MessageType.Error, "Registro no creado con éxito."); } txtBuscar.Text = ""; if (log.IsDebugEnabled) { log.Debug("CrearPelicula Ends"); log.Debug("Result is [" + resultado + "]"); } return resultado; }
/// <summary> /// Retrieves a record from DB about a PeliculaDto object. /// </summary> /// <param name="idPelicula">Parameter to retrieve.</param> /// <returns>One record with data or NULL if none found</returns> public DetallePeliculaDto getPelicula(int idPelicula) { if (log.IsDebugEnabled) { log.Debug("getPelicula Starts"); } SqlTransaction transaction = null; SqlDataReader rdr = null; HandleDatabase hdb = null; DetallePeliculaDto r = null; try { hdb = new HandleDatabase(Settings.Connection); hdb.Open(); String sql = "sp_obtenerPelicula @IDPELICULA"; SqlParameter param = new SqlParameter() { ParameterName = "@IDPELICULA", Value = idPelicula, SqlDbType = SqlDbType.Int }; if (log.IsDebugEnabled) { log.Debug("SQL=[" + sql + "]"); var paramValues = "ParameterName=[" + param.ParameterName + "], Value=[" + param.Value + "], SqlDbType=[" + param.SqlDbType + "]"; log.Debug("Parameter val=[" + paramValues + "]"); } transaction = hdb.BeginTransaction("getPelicula"); rdr = hdb.ExecSelectSQLStmtAsReader(transaction, sql, param); if (rdr.HasRows) { rdr.Read(); r = new DetallePeliculaDto() { idPelicula = Convert.ToInt32(rdr["idPelicula"]), idDetallePelicula = Convert.ToInt32(rdr["idDetallePelicula"]), nombrePelicula = rdr["nombrePelicula"].ToString(), idUsuarioCreador = Convert.ToInt32(rdr["idUsuarioCreador"].ToString()), fechaCreacionPelicula = Convert.ToDateTime(rdr["fechaCreacionPelicula"].ToString()), idGeneroPelicula = Convert.ToInt32(rdr["idGeneroPelicula"].ToString()), imagenCartelera = rdr["imagenCartelera"].ToString(), sinopsis = rdr["sinopsis"].ToString(), urlArticuloEc = rdr["urlArticuloEC"].ToString(), enCartelera = rdr["activo"].ToString(), premiere = rdr["premiere"].ToString() }; } } catch (Exception ex) { if (log.IsFatalEnabled) { log.Fatal("Exception occurred " + ex.Message); log.Fatal("Exception trace=[" + ex.StackTrace + "]"); log.Fatal("Record return is null"); } r = null; } finally { try { if (rdr != null) { rdr.Close(); } if (transaction != null) { transaction.Commit(); } if (hdb != null) { hdb.Close(); } } catch (Exception e) { if (log.IsFatalEnabled) { log.Fatal("Exception occurred " + e.Message); log.Fatal("Exception trace=[" + e.StackTrace + "]"); log.Fatal("Record return is null"); } r = null; } } if (log.IsDebugEnabled) { if (r == null) { log.Debug("Result is NULL"); } else { log.Debug("Result sets to [" + r.ToString() + "]"); } log.Debug("getPelicula Ends"); } return r; }