void SaveData(bool viewChart) { try { ConnectionString conn = new ConnectionString(); string connectionSql = conn.GetConnectionString().ToString(); string user = Context.User.Identity.Name.ToString(); string input = string.Empty; string output = string.Empty; if (!string.IsNullOrEmpty(txtInput.InnerText) && txtInput.InnerText.Length > 3 && txtInput.InnerText.Substring(txtInput.InnerText.Length - 3) == " + ") { input = txtInput.InnerText.Substring(0, txtInput.InnerText.Length - 3); } else { input = txtInput.InnerText; } if (!string.IsNullOrEmpty(txtOuput.InnerText) && txtOuput.InnerText.Length > 3 && txtOuput.InnerText.Substring(txtOuput.InnerText.Length - 3) == " + ") { output = txtOuput.InnerText.Substring(0, txtOuput.InnerText.Length - 3); } else { output = txtOuput.InnerText; } if (isExpressionValid(input) && isExpressionValid(output)) { int MaterialBalanceID = 0; if (!string.IsNullOrEmpty(Request.QueryString["MaterialBalanceID"])) { MaterialBalanceID = int.Parse(Request.QueryString["MaterialBalanceID"]); } else { DataTable dt = SqlHelper.ExecuteDataset(connectionSql, "GetMaterialBalanceByName1", txtMaterialBalanceName.Text).Tables[0]; if (dt.Rows.Count > 0) { lblStatus.ForeColor = Color.Red; lblStatus.Text = "Material Balance already exists with this name."; string script = string.Format("alert('{0}');", "Material Balance already exists with this name."); if (this.Page != null && !this.Page.ClientScript.IsClientScriptBlockRegistered("alert")) { this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "alert", script, true /* addScriptTags */); } return; } } int ID = Convert.ToInt32(SqlHelper.ExecuteScalar(connectionSql, "InsertMaterialBalance", input.Replace("'", "''"), output.Replace("'", "''"), user, txtMaterialBalanceName.Text, MaterialBalanceID)); if (viewChart) { Response.Redirect("MaterialBalanceChart.aspx?MaterialBalanceID=" + ID.ToString()); } else { Response.Redirect("MaterialBalance.aspx"); } //lblStatus.ForeColor = Color.Black; //lblStatus.Text = "Data saved successfully"; } else { lblStatus.ForeColor = Color.Red; lblStatus.Text = "Invalid expression."; string script = string.Format("alert('{0}');", "Invalid Expression."); if (this.Page != null && !this.Page.ClientScript.IsClientScriptBlockRegistered("alert1")) { this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "alert1", script, true /* addScriptTags */); } } } catch (Exception ex) { lblStatus.ForeColor = Color.Red; lblTitle.Text = "Error in saving data. Please try again later."; } }
public void ShowChart() { string InstrumentTag = string.Empty; if (!string.IsNullOrEmpty(Request.QueryString["DeviceName"])) { InstrumentTag = ddlInstrumnet.SelectedValue; } else if (!string.IsNullOrEmpty(Request.QueryString["TagNumber"])) { InstrumentTag = Request.QueryString["TagNumber"]; } else { InstrumentTag = "95-FIC-0379"; } string sql = "select * from InstFuncHierarchy where Tag_No ='" + InstrumentTag + "'"; ConnectionString conn = new ConnectionString(); string connectionSql = conn.GetConnectionString().ToString(); DataTable dt = SqlHelper.ExecuteQuery(connectionSql, sql).Tables[0]; string PITag = ""; if (dt.Rows.Count > 0) { PITag = dt.Rows[0]["PITag"].ToString(); txtInstType.Text = dt.Rows[0]["Service"].ToString(); if (!string.IsNullOrEmpty(PITag)) { DateTime startdate, enddate = new DateTime(); if (string.IsNullOrEmpty(Request.Form["txtStartDate"])) { startdate = CalendarExtender1.SelectedDate.Value; } else { startdate = DateTime.Parse(Request.Form["txtStartDate"]); startdate = startdate.AddHours(int.Parse(ddlHoursStart.SelectedValue)); startdate = startdate.AddMinutes(int.Parse(ddlMinutesStart.SelectedValue)); } if (string.IsNullOrEmpty(Request.Form["txtEndDate"])) { enddate = CalendarExtender2.SelectedDate.Value; } else { enddate = DateTime.Parse(Request.Form["txtEndDate"]); enddate = enddate.AddHours(int.Parse(ddlHoursEnd.SelectedValue)); enddate = enddate.AddMinutes(int.Parse(ddlMinutesEnd.SelectedValue)); } string unit = ""; List <Pi> PIData = getPiData(PITag, startdate, enddate, out unit); showPITrend(PITag, startdate, enddate); if (PIData != null && PIData.Count > 0) { List <decimal> distArray = new List <decimal>(); decimal[] ScoreArray = PIData.Select(x => decimal.Parse(x.value)).ToArray <decimal>(); decimal[] distinctvalues = ScoreArray.Distinct().OrderBy(x => x).ToArray(); Dictionary <decimal, int> seriesArray = new Dictionary <decimal, int>(); //Finds distinct values and orders them in ascending order. decimal min = ScoreArray.Min(); decimal max = ScoreArray.Max(); decimal diff = ((max - min) / 10); decimal current = min; for (int i = 0; i < 10; i++) { distArray.Add(current); decimal count = ScoreArray.Where(x => x >= distArray[i] && x <= (distArray[i] + diff)).Count(); decimal countPercent = (count / ScoreArray.Count()) * 100; int value = (int)Math.Round(distArray[i]); chartFrequency.Series[0].Points.AddXY(value, (int)Math.Round(countPercent)); current = current + diff; //chartFrequency.Series[1].Points.AddXY(value, (int)Math.Round(countPercent)); } chartFrequency.ChartAreas[0].AxisX.Minimum = Convert.ToDouble((int)Math.Round(min)); chartFrequency.ChartAreas[0].AxisX.Maximum = Convert.ToDouble((int)Math.Round(max)); chartFrequency.ChartAreas[0].AxisX.Title = "Value(" + unit + ")"; if (unit.ToLower() == "m3/d" || 1 == 1) { string sqlDiameter = "select * from Instrument_Spec_Data_Stage_1 where Tag_No ='" + InstrumentTag + "'"; DataTable dtDiameter = SqlHelper.ExecuteQuery(connectionSql, sqlDiameter).Tables[0]; string diameter = ""; if (dtDiameter.Rows.Count > 0) { if (!string.IsNullOrEmpty(dtDiameter.Rows[0]["Pipe Id"].ToString())) { diameter = dtDiameter.Rows[0]["Pipe Id"].ToString(); } else if (!string.IsNullOrEmpty(dtDiameter.Rows[0]["Line Size"].ToString())) { diameter = dtDiameter.Rows[0]["Line Size"].ToString(); } else if (!string.IsNullOrEmpty(dtDiameter.Rows[0]["Line Size"].ToString())) { diameter = dtDiameter.Rows[0]["Line Size"].ToString(); } double dDiameter = 0; if (!string.IsNullOrEmpty(diameter) && double.TryParse(diameter, out dDiameter)) { List <double> velocities = new List <double>(); List <double> reynoldNumbers = new List <double>(); foreach (Pi pi in PIData) { double denominator = 0; dDiameter = dDiameter / 0.0254; denominator = (dDiameter) / 2; denominator = denominator * denominator; denominator = denominator * (3.14); double velocity = (double.Parse(pi.value) / denominator) / (24 * 60 * 60); chartVelocity.Series[0].Points.AddXY(pi.time, (int)Math.Round(velocity)); velocities.Add(velocity); double density = 0; double viscosity = 0; if (string.IsNullOrEmpty(dtDiameter.Rows[0]["Viscosity Min"].ToString()) || !double.TryParse(dtDiameter.Rows[0]["Viscosity Min"].ToString(), out viscosity)) { switch (dtDiameter.Rows[0]["fluid"].ToString()) { case "OIL": viscosity = 5; break; case "WATER": viscosity = 1; break; case "Gas": viscosity = 0.01; break; } } else { viscosity = double.Parse(dtDiameter.Rows[0]["Viscosity Min"].ToString()); } if (string.IsNullOrEmpty(dtDiameter.Rows[0]["Operating Density Norm"].ToString()) || !double.TryParse(dtDiameter.Rows[0]["Operating Density Norm"].ToString(), out density)) { switch (dtDiameter.Rows[0]["fluid"].ToString()) { case "OIL": density = 890; break; case "WATER": density = 1000; break; case "Gas": density = 30; break; } } else { density = double.Parse(dtDiameter.Rows[0]["Operating Density Norm"].ToString()); } if (viscosity != 0) { double reynoldNumber = ((density) * (velocity) * (dDiameter)) / viscosity; chartReynoldNumber.Series[0].Points.AddXY(pi.time, (int)Math.Round(reynoldNumber)); reynoldNumbers.Add(reynoldNumber); } } /* if (velocities.Count > 0) * { * chartVelocity.ChartAreas[0].AxisY.Minimum = (int)velocities.Min()-10; * chartVelocity.ChartAreas[0].AxisY.Maximum = (int)velocities.Max()+10; * } * if (reynoldNumbers.Count > 0) * { * chartReynoldNumber.ChartAreas[0].AxisY.Minimum = (int)reynoldNumbers.Min()-10; * chartReynoldNumber.ChartAreas[0].AxisY.Maximum = (int)reynoldNumbers.Max()+10; * }*/ } else { showMessage("Diameter not defined for the instrument."); } } else { showMessage("Diameter not defined for the instrument."); } } else { showMessage("Unit must be in m3/d."); } } //else chartFrequency.DataSource = null; } } }