protected void Button1_Click(object sender, EventArgs e) { if (txtTemperatura.Text.Trim().Length > 0) { FileStream stream; string fileName = Server.MapPath("Temperaturas.txt"); if (lstDepartamentos.Count == 0) { stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); } else { stream = new FileStream(fileName, FileMode.Append, FileAccess.Write); } StreamWriter writer = new StreamWriter(stream); clsTemperatura nuevaTemp = new clsTemperatura(); nuevaTemp.NoIdentificacion = DropDownList1.SelectedValue.ToString(); nuevaTemp.Grados = Convert.ToInt32(txtTemperatura.Text);; nuevaTemp.Fecha = Calendar1.SelectedDate; writer.WriteLine(nuevaTemp.NoIdentificacion); writer.WriteLine(nuevaTemp.Grados); writer.WriteLine(nuevaTemp.Fecha); writer.Close(); this.lstTemperaturas.Add(nuevaTemp); txtTemperatura.Text = ""; DropDownList1.SelectedIndex = 0; string script = "alert(\"Registro agregado exitosamente.\");"; ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true); leer(); } else { string script = "alert(\"Debe agregar una temperatura\");"; ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true); } }
private void leer() { lstDepartamentos = new List <clsDepartamento>(); lstTemperaturas = new List <clsTemperatura>(); lstAux = new List <clsTemp>(); string fileName = Server.MapPath("Departamentos.txt"); FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(stream); while (reader.Peek() > -1) { clsDepartamento departamentoTemp = new clsDepartamento(); departamentoTemp.NoIdentificacion = reader.ReadLine(); departamentoTemp.Nombre = reader.ReadLine(); lstDepartamentos.Add(departamentoTemp); } reader.Close(); string fileName2 = Server.MapPath("Temperaturas.txt"); FileStream stream2 = new FileStream(fileName2, FileMode.Open, FileAccess.Read); StreamReader reader2 = new StreamReader(stream2); while (reader2.Peek() > -1) { clsTemperatura temperaturaTemp = new clsTemperatura(); temperaturaTemp.NoIdentificacion = reader2.ReadLine(); temperaturaTemp.Grados = Convert.ToInt32(reader2.ReadLine()); temperaturaTemp.Fecha = Convert.ToDateTime(reader2.ReadLine()); lstTemperaturas.Add(temperaturaTemp); } reader2.Close(); foreach (var l in lstTemperaturas) { foreach (var d in lstDepartamentos) { if (l.NoIdentificacion == d.NoIdentificacion) { clsTemp aux = new clsTemp(); aux.Temp = l.Grados; aux.Nombre = d.Nombre; aux.Cent = aux.Temp + "°C"; lstAux.Add(aux); } } } if (!IsPostBack) { if (lstDepartamentos.Count > 0) { DropDownList1.Items.Clear(); DropDownList1.DataValueField = "NoIdentificacion"; DropDownList1.DataTextField = "NoIdentificacion"; DropDownList1.DataSource = lstDepartamentos; DropDownList1.DataBind(); mostrarDep(); } } if (lstTemperaturas.Count > 0) { GridView1.DataSource = null; GridView1.DataSource = lstTemperaturas; GridView1.DataBind(); } }
private void leer() { lstDepartamentos = new List <clsDepartamento>(); lstTemperaturas = new List <clsTemperatura>(); lstAux = new List <clsTemp>(); string fileName = Server.MapPath("Departamentos.txt"); FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(stream); while (reader.Peek() > -1) { clsDepartamento departamentoTemp = new clsDepartamento(); departamentoTemp.NoIdentificacion = reader.ReadLine(); departamentoTemp.Nombre = reader.ReadLine(); lstDepartamentos.Add(departamentoTemp); } reader.Close(); string fileName2 = Server.MapPath("Temperaturas.txt"); FileStream stream2 = new FileStream(fileName2, FileMode.Open, FileAccess.Read); StreamReader reader2 = new StreamReader(stream2); while (reader2.Peek() > -1) { clsTemperatura temperaturaTemp = new clsTemperatura(); temperaturaTemp.NoIdentificacion = reader2.ReadLine(); temperaturaTemp.Grados = Convert.ToInt32(reader2.ReadLine()); temperaturaTemp.Fecha = Convert.ToDateTime(reader2.ReadLine()); lstTemperaturas.Add(temperaturaTemp); } reader2.Close(); temperaturaProm = 0; foreach (var l in lstTemperaturas) { foreach (var d in lstDepartamentos) { if (l.NoIdentificacion == d.NoIdentificacion) { clsTemp aux = new clsTemp(); aux.Temp = l.Grados; aux.Nombre = d.Nombre; aux.Cent = aux.Cent + "°C";// solo aparecera °C // antes: aux.Grados + "°C" para que apareciera valor°C // no se pudo poner una columna en visible false, entonces // se modificó este código para que solo aparezca el "°C" lstAux.Add(aux); } } } if (lstAux2 != null) { dtgDatos.DataSource = null; dtgDatos.DataSource = lstAux2; dtgDatos.DataBind(); lstAux2 = null; } if (!IsPostBack) { if (lstDepartamentos.Count > 0) { DropDownList1.Items.Clear(); DropDownList1.DataValueField = "NoIdentificacion"; DropDownList1.DataTextField = "Nombre"; DropDownList1.DataSource = lstDepartamentos; DropDownList1.DataBind(); mostrarTemp(); dtgDatos.DataSource = null; dtgDatos.DataSource = lstAux; dtgDatos.DataBind(); } } foreach (var l in lstTemperaturas) { temperaturaProm += l.Grados; } temperaturaProm = temperaturaProm / lstTemperaturas.Count; lblTemp.Text = temperaturaProm + " °C"; }