public static IEnumerable <Preguntas_Aleatorias> PreguntasPorPonderacion(string CodSec, string Ponderacion, string CodUser) { List <Preguntas_Aleatorias> RQ = new List <Preguntas_Aleatorias>(); string CnnStr = ConfigurationManager.ConnectionStrings["CnnStr"].ConnectionString; string sql = "Select Distinct Numero_Pregunta, Texto_Pregunta from Preguntas_Aleatorias Pal " + "Join Secciones Sec on Pal.Codigo_Seccion = Sec.Codigo_Seccion " + "Where Sec.Codigo_Seccion = '" + CodSec + "' And " + "And Pal.Ponderacion_P = " + Ponderacion; using SqlConnection Cnn = new SqlConnection(CnnStr); using SqlCommand cmd = new SqlCommand { CommandText = sql, Connection = Cnn }; Cnn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { if (dr.HasRows) { while (dr.Read()) { Preguntas_Aleatorias pal = new Preguntas_Aleatorias() { Codigo_Seccion = CodSec, Numero_Pregunta = int.Parse(dr.GetString(0)), Texto_Pregunta = dr.GetString(1), }; RQ.Add(pal); } } } return(RQ); }
public ActionResult Delete(RQViewModel Rq) { try { if (ModelState.IsValid) { using var Db = new BD_EvaluacionEntities(); Preguntas_Aleatorias oRQ = Db.Preguntas_Aleatorias.Where(i => i.Codigo_Seccion == Rq.Codigo_Seccion && i.Numero_Pregunta == Rq.Numero_Pregunta).SingleOrDefault(); Db.Entry(oRQ).State = System.Data.Entity.EntityState.Deleted; Db.SaveChanges(); mensaje = "Ok"; } else { string errors = string.Empty; foreach (var item in ModelState.Values) { if (item.Errors.Count > 0) { mensaje += string.Format("{0} \n", item.Errors[0].ErrorMessage); } } mensaje += " Contacte al Administrador"; } } catch (Exception e) { mensaje = "Ocurrió el siguiente error " + e.Message + " Contactar al administrador"; } return(View(new { codSection = Rq.Codigo_Seccion, quesNo = Rq.Numero_Pregunta, mensaje })); }
public ActionResult Create(RQViewModel Rq) { try { //Validación del Modelo if (ModelState.IsValid) { #region Graba Datos using var bd = new BD_EvaluacionEntities(); Preguntas_Aleatorias oRQ = new Preguntas_Aleatorias { Codigo_Seccion = Rq.Codigo_Seccion.ToUpper(), Numero_Pregunta = Rq.Numero_Pregunta, Texto_Pregunta = Rq.Texto_Pregunta, Ponderacion_P = Rq.Ponderacion_P }; bd.Preguntas_Aleatorias.Add(oRQ); bd.SaveChanges(); mensaje = "Ok"; #endregion } else { #region Errores de Modelo string errors = string.Empty; foreach (var item in ModelState.Values) { if (item.Errors.Count > 0) { mensaje += string.Format("{0} \n", item.Errors[0].ErrorMessage); } } mensaje += " Contacte al Administrador"; #endregion } } catch (Exception e) { mensaje = "Ocurrió el siguiente error " + e.Message + " Contactar al administrador"; } return(RedirectToAction("Create", "RandomQuestion", new { mensaje })); }
public ActionResult Edit(RQViewModel Rq) { ViewBag.Sections = new SelectList(Tools.LeerSecciones(), "Codigo_Seccion", "Nombre_Seccion", ""); try { if (ModelState.IsValid) { using BD_EvaluacionEntities Db = new BD_EvaluacionEntities(); Preguntas_Aleatorias oRQ = Db.Preguntas_Aleatorias.Where(i => i.Codigo_Seccion == Rq.Codigo_Seccion && i.Numero_Pregunta == Rq.Numero_Pregunta).SingleOrDefault(); oRQ.Numero_Pregunta = Rq.Numero_Pregunta; oRQ.Texto_Pregunta = Rq.Texto_Pregunta; oRQ.Ponderacion_P = Rq.Ponderacion_P; Db.Entry(oRQ).State = System.Data.Entity.EntityState.Modified; Db.SaveChanges(); mensaje = "Ok"; } else { string errors = string.Empty; foreach (var item in ModelState.Values) { if (item.Errors.Count > 0) { mensaje += string.Format("{0} \n", item.Errors[0].ErrorMessage); } } mensaje += " Contacte al Administrador"; } return(View(new { codSection = Rq.Codigo_Seccion, askNo = Rq.Numero_Pregunta, mensaje })); } catch (Exception e) { mensaje = "Ocurrió el siguiente error " + e.Message + " Contactar al administrador"; return(View(new { mensaje })); } }
public static IEnumerable <Preguntas_Aleatorias> LeerPreguntasAleatorias(string codSec, int askNo) { string texto; List <Preguntas_Aleatorias> PA = new List <Preguntas_Aleatorias>(); string CnnStr = ConfigurationManager.ConnectionStrings["CnnStr"].ConnectionString; using SqlConnection Cnn = new SqlConnection(CnnStr); using SqlCommand cmd = new SqlCommand(); texto = "Select * from Preguntas_Aleatorias "; if (codSec != "") { texto += " where Codigo_Seccion = '" + codSec + "'"; } if (askNo > 0) { texto += " and Numero_Pregunta = " + askNo; } cmd.CommandText = texto; cmd.Connection = Cnn; Cnn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { Preguntas_Aleatorias rq = new Preguntas_Aleatorias() { Numero_Pregunta = dr.GetInt32(1), Texto_Pregunta = dr.GetString(2), Ponderacion_P = dr.GetDecimal(3) }; PA.Add(rq); } } return(PA); }
public List <RQExcelModel> ReadRQExcel(string FilePath) { List <RQExcelModel> excelData = new List <RQExcelModel>(); try { FileInfo existingFile = new FileInfo(FilePath); using ExcelPackage package = new ExcelPackage(existingFile); ExcelWorksheet wrkSheet = package.Workbook.Worksheets[1]; int rowCount = wrkSheet.Dimension.End.Row; using (BD_EvaluacionEntities db = new BD_EvaluacionEntities()) { try { for (int row = 2; row <= rowCount; row++) { if (wrkSheet.Cells[row, 1].Value != null) { int total = 0; var CodSec = wrkSheet.Cells[row, 1].Value.ToString().Trim().ToUpper(); int res = Tools.ValidaDominios(CodSec); if (res == 0) { using BD_EvaluacionEntities bd = new BD_EvaluacionEntities(); var oSec = new Secciones { Codigo_Seccion = CodSec, Nombre_Seccion = CodSec, Ponderacion_S = decimal.Parse("30,0"), IdState = 1 }; bd.Secciones.Add(oSec); bd.SaveChanges(); } decimal ponderacion = 0; if (wrkSheet.Cells[row, 4].Value != null) { ponderacion = Decimal.Parse(wrkSheet.Cells[row, 4].Value.ToString()); } var oRQ = new Preguntas_Aleatorias { Codigo_Seccion = CodSec, Numero_Pregunta = Convert.ToInt32(wrkSheet.Cells[row, 2].Value.ToString().Trim()), Texto_Pregunta = wrkSheet.Cells[row, 3].Value.ToString().Trim(), Ponderacion_P = ponderacion }; res = Tools.ValidaPreguntas(CodSec, oRQ.Numero_Pregunta); if (res == 0) { total += 1; db.Preguntas_Aleatorias.Add(oRQ); db.SaveChanges(); } else { db.Entry(oRQ).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } excelData.Add(new RQExcelModel() { Codigo_Seccion = CodSec, Numero_Pregunta = oRQ.Numero_Pregunta, Texto_Pregunta = oRQ.Texto_Pregunta, Ponderacion_P = oRQ.Ponderacion_P }); if (total == 0) { excelData.Clear(); excelData.Add(new RQExcelModel() { Codigo_Seccion = "Error", Texto_Pregunta = "Los registros ya existen, valide la información", Numero_Pregunta = 0 }); } } } //bd.SaveChanges(); } catch (Exception ex) { string mensaje = ex.InnerException.InnerException.Message; excelData.Clear(); excelData.Add(new RQExcelModel() { Codigo_Seccion = "Error", Texto_Pregunta = mensaje + " Valide la información a Ingresar o Contáctese con el administrador", Numero_Pregunta = 0, Ponderacion_P = 0 }); } } } catch (Exception ex) { string mensaje = ex.InnerException.InnerException.Message; excelData.Clear(); excelData.Add(new RQExcelModel() { Codigo_Seccion = "Error", Texto_Pregunta = mensaje + " Contactar al administrador", Numero_Pregunta = 0, Ponderacion_P = 0 }); } return(excelData); }