I have an MVC that takes values from a form, sends them to a dll, to have logic performed on them & assigned to a model. The model is then returned to the view. In localhost it is successful and I get the following result:
However when I publish the webapp to an azure instance, it doesn't return any of the values. You can see it here: http://levelupsite.azurewebsites.net/ or see the image here
Does anyone know what could cause this? I will attach the code below:
Controller:
[HttpPost]
public ActionResult CBC(System.Web.Mvc.FormCollection form, FFAC model)
{
//recieve values from form
var email = escapeCharactersspace((form["email"].ToString()));
var Gender = Convert.ToString(model.UserGender);
var UserID = escapeCharactersspace((form["username"].ToString()));
var Neutrophils = escapeCharactersspace(form["Neutrophils"]);
var Lymphocytes = escapeCharactersspace(form["Lympthocytes"]);
var Monocytes = escapeCharactersspace(form["Monocytes"]);
var Eosinophils = escapeCharactersspace(form["Eosinophils"]);
var Basophils = escapeCharactersspace(form["Basophils"]);
var Platelets = (escapeCharactersspace(form["Platelets"]));
var Haematocrit = escapeCharactersspace(form["Haematocrit"]);
var Haemoglobin = escapeCharactersspace(form["Haemoglobin"]);
var MCV = escapeCharactersspace(form["MCV"]);
var MCH = (escapeCharactersspace(form["MCH"]));
var MCHC = escapeCharactersspace(form["MCHC"]);
//turn form to array
decimal[] cbcInputs = { Convert.ToDecimal(Neutrophils), Convert.ToDecimal(Lymphocytes), Convert.ToDecimal(Monocytes), Convert.ToDecimal(Eosinophils), Convert.ToDecimal(Basophils), Convert.ToDecimal(Platelets), Convert.ToDecimal(Haematocrit), Convert.ToDecimal(Haemoglobin), Convert.ToDecimal(MCV), Convert.ToDecimal(MCH), Convert.ToDecimal(MCHC) };
//create instance of model
var scfiae = new FIAECBC();
//create instance of external class library
var fiae = new FIAEngine();
//send inputs & model instance to external class library to perform logic
fiae.setcbcvals(cbcInputs, UserID, Gender, scfiae);
//return the get results page
return View("GetResults", scfiae);
}
FIAEngine - dll that performs the logic
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharedComponents;
using static SharedComponents.FIAECBC;
namespace FIAE
{
public class FIAEngine
{
decimal d_Neutrophils;
decimal d_Lymphocytes;
decimal d_Monocytes;
decimal d_Eosinophils;
decimal d_Basophils;
decimal d_Platelets;
decimal d_Haematocrit;
decimal d_Haemoglobin;
decimal d_MCV;
decimal d_MCH;
decimal d_MCHC;
string UserID;
string Gender;
string Neutrophils;
string Lymphocytes;
string Monocytes;
string Eosinophils;
string Basophils;
string Platelets;
string Haematocrit;
string Haemoglobin;
string MCV;
string MCH;
string MCHC;
string rag_Neutrophils;
string rag_Lymphocytes;
string rag_Monocytes;
string rag_Eosinophils;
string rag_Basophils;
string rag_Platelets;
string rag_Haematocrit;
string rag_Haemoglobin;
string rag_MCV;
string rag_MCH;
string rag_MCHC;
public void Setcbcfilevals(FIAECBC cbcmodel)
{
}
public void setcbcvals(decimal[] inputs, string _UserID, string _Gender, FIAECBC cbcmodel)
{
d_Neutrophils = inputs[0];
d_Lymphocytes = inputs[1];
d_Monocytes = inputs[2];
d_Eosinophils = inputs[3];
d_Basophils = inputs[4];
d_Platelets = inputs[5];
d_Haematocrit = inputs[6];
d_Haemoglobin = inputs[7];
d_MCV = inputs[8];
d_MCH = inputs[9];
d_MCHC = inputs[10];
UserID = _UserID;
Gender = _Gender;
Neutrophils = "Neutrophils";
Lymphocytes = "Lymphocytes";
Monocytes = "Monocytes";
Eosinophils = "Eosinophils";
Basophils = "Basophils";
Platelets = "Platelets";
Haematocrit = "Haematocrit";
Haemoglobin = "Haemoglobin";
MCV = "MCV";
MCH = "MCH";
MCHC = "MCHC";
rag_Neutrophils = "i";
rag_Lymphocytes = "i";
rag_Monocytes = "i";
rag_Eosinophils = "i";
rag_Basophils = "i";
rag_Platelets = "i";
rag_Haematocrit = "i";
rag_Haemoglobin = "i";
rag_MCV = "i";
rag_MCH = "i";
rag_MCHC = "i";
try
{
//male calculations
if (Gender == "Male")
{
rag_Neutrophils = FindMaleRAG(d_Neutrophils, 1);
rag_Lymphocytes = FindMaleRAG(d_Lymphocytes, 2);
rag_Monocytes = FindMaleRAG(d_Monocytes, 3);
rag_Eosinophils = FindMaleRAG(d_Eosinophils, 4);
rag_Basophils = FindfeMaleRAG(d_Basophils, 5);
rag_Platelets = FindMaleRAG(d_Platelets, 6);
rag_Haematocrit = FindMaleRAG(d_Haematocrit, 7);
rag_Haemoglobin = FindMaleRAG(d_Haemoglobin, 8);
rag_MCV = FindMaleRAG(d_MCV, 9);
rag_MCH = FindMaleRAG(d_MCH, 10);
rag_MCHC = FindMaleRAG(d_MCHC, 11);
//set view model values to the form values
cbcmodel.d_Neutrophils = d_Neutrophils;
cbcmodel.d_Lymphocytes = d_Lymphocytes;
cbcmodel.d_Monocytes = d_Monocytes;
cbcmodel.d_Eosinophils = d_Eosinophils;
cbcmodel.d_Basophils = d_Basophils;
cbcmodel.d_Platelets = d_Platelets;
cbcmodel.d_Haematocrit = d_Haematocrit;
cbcmodel.d_Haemoglobin = d_Haemoglobin;
cbcmodel.d_MCV = d_MCV;
cbcmodel.d_MCH = d_MCH;
cbcmodel.d_MCHC = d_MCHC;
cbcmodel.Neutrophils = Neutrophils;
cbcmodel.Lymphocytes = Lymphocytes;
cbcmodel.Monocytes = Monocytes;
cbcmodel.Eosinophils = Eosinophils;
cbcmodel.Basophils = Basophils;
cbcmodel.Platelets = Platelets;
cbcmodel.Haematocrit = Haematocrit;
cbcmodel.Haemoglobin = Haemoglobin;
cbcmodel.MCV = MCV;
cbcmodel.MCH = MCH;
cbcmodel.MCHC = MCHC;
cbcmodel.rag_Neutrophils = rag_Neutrophils;
cbcmodel.rag_Lymphocytes = rag_Lymphocytes;
cbcmodel.rag_Monocytes = rag_Monocytes;
cbcmodel.rag_Eosinophils = rag_Eosinophils;
cbcmodel.rag_Basophils = rag_Basophils;
cbcmodel.rag_Platelets = rag_Platelets;
cbcmodel.rag_Haematocrit = rag_Haematocrit;
cbcmodel.rag_Haemoglobin = rag_Haematocrit;
cbcmodel.rag_MCV = rag_MCV;
cbcmodel.rag_MCH = rag_MCH;
cbcmodel.rag_MCHC = rag_MCHC;
}
else if (Gender == "Female")
{
rag_Neutrophils = FindfeMaleRAG(d_Neutrophils, 1);
rag_Lymphocytes = FindfeMaleRAG(d_Lymphocytes, 2);
rag_Monocytes = FindfeMaleRAG(d_Monocytes, 3);
rag_Eosinophils = FindfeMaleRAG(d_Eosinophils, 4);
rag_Basophils = FindfeMaleRAG(d_Basophils, 5);
rag_Platelets = FindfeMaleRAG(d_Platelets, 6);
rag_Haematocrit = FindfeMaleRAG(d_Haematocrit, 7);
rag_Haemoglobin = FindfeMaleRAG(d_Haemoglobin, 8);
rag_MCV = FindfeMaleRAG(d_MCV, 9);
rag_MCH = FindfeMaleRAG(d_MCH, 10);
rag_MCHC = FindfeMaleRAG(d_MCHC, 11);
//set view model values to the form values
cbcmodel.d_Neutrophils = d_Neutrophils;
cbcmodel.d_Lymphocytes = d_Lymphocytes;
cbcmodel.d_Monocytes = d_Monocytes;
cbcmodel.d_Eosinophils = d_Eosinophils;
cbcmodel.d_Basophils = d_Basophils;
cbcmodel.d_Platelets = d_Platelets;
cbcmodel.d_Haematocrit = d_Haematocrit;
cbcmodel.d_Haemoglobin = d_Haemoglobin;
cbcmodel.d_MCV = d_MCV;
cbcmodel.d_MCH = d_MCH;
cbcmodel.d_MCHC = d_MCHC;
cbcmodel.Neutrophils = Neutrophils;
cbcmodel.Lymphocytes = Lymphocytes;
cbcmodel.Monocytes = Monocytes;
cbcmodel.Eosinophils = Eosinophils;
cbcmodel.Basophils = Basophils;
cbcmodel.Platelets = Platelets;
cbcmodel.Haematocrit = Haematocrit;
cbcmodel.Haemoglobin = Haemoglobin;
cbcmodel.MCV = MCV;
cbcmodel.MCH = MCH;
cbcmodel.MCHC = MCHC;
cbcmodel.rag_Neutrophils = rag_Neutrophils;
cbcmodel.rag_Lymphocytes = rag_Lymphocytes;
cbcmodel.rag_Monocytes = rag_Monocytes;
cbcmodel.rag_Eosinophils = rag_Eosinophils;
cbcmodel.rag_Basophils = rag_Basophils;
cbcmodel.rag_Platelets = rag_Platelets;
cbcmodel.rag_Haematocrit = rag_Haematocrit;
cbcmodel.rag_Haemoglobin = rag_Haematocrit;
cbcmodel.rag_MCV = rag_MCV;
cbcmodel.rag_MCH = rag_MCH;
cbcmodel.rag_MCHC = rag_MCHC;
}
}
catch (Exception ex)
{
}
//return inputs;
}
public string FindMaleRAG(decimal i, int x)
{
String connString = Convert.ToString(ConfigurationManager.ConnectionStrings["SQLServerCon"]);
//for each row, do this
//find the threshld values
String thresholdquery = #"select * from dbo.malethreshold where ID = " + x;
using (var conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(thresholdquery, conn))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
//compare threshold values t posted values from form
string composite = Convert.ToString(reader[1]);
decimal redlow = Convert.ToDecimal(reader[2]);
decimal greenlow = Convert.ToDecimal(reader[3]);
decimal greenhigh = Convert.ToDecimal(reader[4]);
decimal redhigh = Convert.ToDecimal(reader[5]);
if (i < redlow)
{
// Red low
return ("red");
}
else if (i > redlow && i < greenlow)
{
// Amber Low
return ("orange");
}
else if (i >= greenlow && i <= greenhigh)
{
//green
return ("green");
}
else if (i > greenhigh && i < redhigh)
{
//amber high
return ("orange");
}
else if (i > redhigh)
{
// Redhigh
return ("red");
}
else
{
//sorting error
return ("error in sorting");
}
}
}
}
}
return ("sorting error");
}
public string FindfeMaleRAG(decimal i, int x)
{
String connString = Convert.ToString(ConfigurationManager.ConnectionStrings["SQLServerCon"]);
//for each row, do this
//find the threshld values
String thresholdquery = #"select * from dbo.malethreshold where ID = " + x;
using (var conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(thresholdquery, conn))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
//compare threshold values t posted values from form
string composite = Convert.ToString(reader[1]);
decimal redlow = Convert.ToDecimal(reader[2]);
decimal greenlow = Convert.ToDecimal(reader[3]);
decimal greenhigh = Convert.ToDecimal(reader[4]);
decimal redhigh = Convert.ToDecimal(reader[5]);
if (i < redlow)
{
// Red low
return ("red");
}
else if (i > redlow && i < greenlow)
{
// Amber Low
return ("orange");
}
else if (i >= greenlow && i <= greenhigh)
{
//green
return ("green");
}
else if (i > greenhigh && i < redhigh)
{
//amber high
return ("orange");
}
else if (i > redhigh)
{
// Redhigh
return ("red");
}
else
{
//sorting error
return ("error in sorting");
}
}
}
}
}
return ("sorting error");
}
}
}
FIAECBC model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace SharedComponents
{
public class FIAECBC
{
public string Neutrophils { get; set; }
public string Lymphocytes { get; set; }
public string Monocytes { get; set; }
public string Eosinophils { get; set; }
public string Basophils { get; set; }
public string Platelets { get; set; }
public string Haematocrit { get; set; }
public string Haemoglobin { get; set; }
public string MCV { get; set; }
public string MCH { get; set; }
public string MCHC { get; set; }
public decimal d_Neutrophils { get; set; }
public decimal d_Lymphocytes { get; set; }
public decimal d_Monocytes { get; set; }
public decimal d_Eosinophils { get; set; }
public decimal d_Basophils { get; set; }
public decimal d_Platelets { get; set; }
public decimal d_Haematocrit { get; set; }
public decimal d_Haemoglobin { get; set; }
public decimal d_MCV { get; set; }
public decimal d_MCH { get; set; }
public decimal d_MCHC { get; set; }
public string rag_Neutrophils { get; set; }
public string rag_Lymphocytes { get; set; }
public string rag_Monocytes { get; set; }
public string rag_Eosinophils { get; set; }
public string rag_Basophils { get; set; }
public string rag_Platelets { get; set; }
public string rag_Haematocrit { get; set; }
public string rag_Haemoglobin { get; set; }
public string rag_MCV { get; set; }
public string rag_MCH { get; set; }
public string rag_MCHC { get; set; }
public Gender UserGender { get; set; }
public static IEnumerable<SelectListItem> GetSelectItems()
{
yield return new SelectListItem { Text = "Male", Value = "Male" };
yield return new SelectListItem { Text = "Female", Value = "Female" };
}
public bool Bookkeeping { get; set; }
}
public enum Gender
{
Male,
Female
}
}
Related
I'm currently trying to scrape a cannabis strain database as it is no longer being maintained. I seem to be running into an issue where table rows are skipped in my logic but it really doesn't make sense, it's like a break is being called when I'm iterating through the //tr elements of the table that is storing the chemical reports.
Is it something like the δ α symbols in the next row. I've tried regex replacing them out to now luck. Any help would be appreciated all code is in a single class console app.
Issue is in the table.ChildNodes not iterating all the way through. Located in the ParseChemical() method.
Sample Page: http://ocpdb.pythonanywhere.com/ocpdb/420/
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.Design;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Reflection.Emit;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Microsoft.VisualBasic.CompilerServices;
namespace CScrape
{
class Program
{
private static readonly HttpClient Client = new HttpClient();
private static readonly string BaseUri = "http://ocpdb.pythonanywhere.com/ocpdb/";
private static int _startId = 420;
private static int _endId = 1519;
private static List<Lab> _labs = new List<Lab>();
private static List<ChemicalItem> _chemicalItems = new List<ChemicalItem>();
private static List<UnitOfMeasure> _uoms = new List<UnitOfMeasure>();
private static List<Strain> _strains = new List<Strain>();
static void Main(string[] args)
{
Client.DefaultRequestHeaders.Accept.Clear();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
_uoms.AddRange(GetUoms());
for (var i = _startId; i <= _endId; i++)
{
var result = GetUri($"{BaseUri}{i}").Result;
_strains.Add(ParseChemical(result));
}
}
private static long AddChemicalItem(ChemicalItem item)
{
if (ChemicalExists(item.Symbol))
return _chemicalItems.FirstOrDefault(ci => ci.Symbol == item.Symbol)?.Id ?? -1;
item.Id = _chemicalItems.Count + 1;
_chemicalItems.Add(item);
return item.Id;
}
private static void UpdateChemicalItem(ChemicalItem item)
{
if (!ChemicalExists(item.Symbol)) return;
var index = _chemicalItems.IndexOf(item);
if (!(index >= 0)) return;
_chemicalItems.RemoveAt(index);
AddChemicalItem(item);
}
private static long AddLab(Lab lab)
{
if (LabExists(lab.Name))
return _labs.FirstOrDefault(l => l.Name == lab.Name)?.Id ?? -1;
lab.Id = _labs.Count + 1;
_labs.Add(lab);
return lab.Id;
}
private static async Task<string> GetUri(string uri)
{
var response = await Client.GetByteArrayAsync(uri);
return Encoding.UTF8.GetString(response, 0, response.Length - 1);
}
private static Strain ParseChemical(string html)
{
html = Regex.Replace(html, #"Δ", "Delta");
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var strain = new Strain();
strain.Reports ??= new List<ChemicalReport>();
try
{
strain.Name = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div[1]/div[1]/h3/b").InnerText;
}
catch (Exception e)
{
// TODO: DOcument Exception
Console.WriteLine(e.Message);
}
if (string.IsNullOrWhiteSpace(strain.Name)) return null;
try
{
var ocpId = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div[1]/div[2]/p/b/text()[1]");
strain.OcpId = SanitizeHtml(ocpId.InnerText).Split(':')[1];
}
catch (Exception e)
{
// TODO: Document Exception
Console.WriteLine(e.Message);
}
if (string.IsNullOrWhiteSpace(strain.OcpId)) return null;
try
{
var date = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div[1]/div[2]/p/text()");
}
catch (Exception e)
{
// TODO: Document Exception
Console.WriteLine(e.Message);
}
var chemReport = new ChemicalReport();
chemReport.Items ??= new List<ReportItem>();
try
{
var table = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div[2]/div[1]/table/tbody");
var children = table.ChildNodes.ToList();
// On the sample page there are 200 children here
// However it only interates through the first few and then just breaks out of the loop
foreach (var child in children)
{
var name = child.Name;
if (child.Name == "tr")
{
var infos = child.SelectNodes("th|td");
foreach (var info in infos)
{
if(string.IsNullOrWhiteSpace(info.InnerText)) continue;
if (info.InnerText.Contains("Report")) continue;
if (double.TryParse(info.InnerText, out var isNumber))
{
var last = chemReport.Items.LastOrDefault();
if (last == null) continue;
if (last.Value <= 0.0000) last.Value = isNumber;
else
{
var further = chemReport.Items.ToArray()[chemReport.Items.Count - 2];
if (further.Value <= 0.0000)
further.Value = isNumber;
}
continue;
}
var _ = new ChemicalItem
{
Name = info.InnerText,
Symbol = info.InnerText
};
_.Id = AddChemicalItem(_);
var report = new ReportItem
{
Chemical = _,
ChemicalItemId = _.Id,
UnitOfMeasureId = 1,
UoM = GetUoms()[0]
};
chemReport.Items.Add(report);
}
}
}
strain.Reports.Add(chemReport);
}
catch (Exception e)
{
// TODO: Document exception
Console.Write(e.Message);
}
return strain;
}
private static List<UnitOfMeasure> GetUoms()
{
return new List<UnitOfMeasure>
{
new UnitOfMeasure {Name = "Milligrams per Gram", Symbol = "mg/g"},
new UnitOfMeasure {Name = "Percent", Symbol = "%"}
};
}
private static string SanitizeHtml(string text, string replacement = "")
{
return Regex.Replace(text, #"<[^>]+>| |α|\n|\t", replacement);
}
private static string GetLabName(string[] split)
{
var strip = split[0].Split(':')[1];
for(var i = 1; i < split.Length - 2; i ++)
{
if (string.IsNullOrWhiteSpace(split[i])) break;
strip += $" {split[i]}";
}
return strip;
}
private static string GetSampleId(string[] split)
{
var found = false;
foreach (var item in split)
{
if (found)
return item.Split(':')[1];
if (item == "Sample") found = true;
}
return "NA";
}
private static bool LabExists(string name)
{
return _labs.Any(lab => lab.Name == name);
}
private static bool ChemicalExists(string name)
{
return _chemicalItems.Any(ci => ci.Symbol == name);
}
private static ReportItem GetReportItem(string text)
{
if (string.IsNullOrWhiteSpace(text)) return null;
ReportItem ri = null;
try
{
var clean = SanitizeHtml(text);
var check = 0;
var split = clean.Split(':');
var label = split[0];
if (string.IsNullOrWhiteSpace(label)) return null;
if (double.TryParse(label, out var invalidType)) return null;
var val = string.Empty;
if (split.Length == 1)
{
if (split[0].Contains("Total"))
{
Regex re = new Regex(#"([a-zA-Z]+)(\d+)");
Match result = re.Match(split[0]);
label = result.Groups[1].Value;
val = result.Groups[2].Value;
}
}
if(split.Length > 1)
val = split[1];
if (!ChemicalExists(label)) AddChemicalItem(new ChemicalItem {Id = _chemicalItems.Count + 1,Symbol = label});
ri = new ReportItem();
ri.Chemical = _chemicalItems.FirstOrDefault(ci => ci.Symbol == label);
ri.UoM = val.Contains("%")
? _uoms.FirstOrDefault(uom => uom.Symbol == "%")
: _uoms.FirstOrDefault(uom => uom.Symbol == "mg/g");
if (string.IsNullOrWhiteSpace(val)) return ri;
var value = val.Contains("%") ? split[1].Substring(0, val.Length - 1) : val;
ri.Value = Convert.ToDouble(value);
}
catch (Exception e)
{
// TODO: Document Exception
Console.WriteLine(e.Message);
}
return ri;
}
//private static ChemicalItem GetChemicalItem(string text)
//{
//}
public class Strain
{
public long Id { get; set; }
public string Name { get; set; }
public DateTime Created { get; set; }
public string OcpId { get; set; }
public bool IsHidden { get; set; } = false;
public virtual ICollection<ChemicalReport> Reports { get; set; }
}
public class Lab
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ChemicalReport> Reports { get; set; }
}
public class ChemicalReport
{
public long Id { get; set; }
[ForeignKey("Lab")]
public long LabId { get; set; }
public virtual Lab Lab { get; set; }
public string SampleId { get; set; }
public DateTime Created { get; set; }
public virtual ICollection<ReportItem> Items { get; set; }
[ForeignKey("Strain")]
public long StrainId { get; set; }
public virtual Strain Strain { get; set; }
}
public class ChemicalItem
{
public long Id { get; set; }
public string Name { get; set; }
public string Symbol { get; set; }
}
public class ReportItem
{
public long Id { get; set; }
[ForeignKey("Chemical")]
public long ChemicalItemId { get; set; }
public virtual ChemicalItem Chemical { get; set; }
public double Value { get; set; }
[ForeignKey("UoM")]
public long UnitOfMeasureId { get; set; }
public virtual UnitOfMeasure UoM { get; set; }
}
public class UnitOfMeasure
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Symbol { get; set; }
}
}
}
I am still having real performance issue in a list there our 38 thousand entrys and I need to map them to another table for export I am thinking of moving it to stored proc but still worried what performance would be like there. The code works but takes a very long time to execute and wish to turn it into a stored proc.
private List<TradeItemsExport> MapTradeItems(List<TradeItems> tradeItem)
{
List<TradeItemsExport> retList = new List<TradeItemsExport>();
try
{
var StockImport = new StockItemExported();
List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode();
StandardLookUpList sport = new StandardLookUpList();
StandardLookUpList gender = new StandardLookUpList();
StandardLookUpList colour = new StandardLookUpList();
StandardLookUpList Size = new StandardLookUpList();
StandardLookUpList categorycode = new StandardLookUpList();
StandardLookUpList categorydesc = new StandardLookUpList();
StandardLookUpList subcategorycode = new StandardLookUpList();
StandardLookUpList subcategorydesc = new StandardLookUpList();
StandardLookUpList brandcode = new StandardLookUpList();
StandardLookUpList branddesc = new StandardLookUpList();
using (var db = new liveEntities1())
{
int count = 0;
foreach (var item in tradeItem)
{
count++;
bool hasprocessed = HasTransactionBeenProcessed(item.ItemCode);
if (hasprocessed == false)
{
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Any())
{
sport = codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport);
gender = codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender);
colour = codesForThisItem.FirstOrDefault(x => x.code == Constants.Colour);
Size = codesForThisItem.FirstOrDefault(x => x.code == Constants.Size);
categorycode = codesForThisItem.FirstOrDefault(x => x.code == Constants.Category);
categorydesc = codesForThisItem.FirstOrDefault(x => x.code == Constants.Category);
subcategorycode = codesForThisItem.FirstOrDefault(x => x.code == Constants.SubCategory);
subcategorydesc = codesForThisItem.FirstOrDefault(x => x.code == Constants.SubCategory);
brandcode = codesForThisItem.FirstOrDefault(x => x.code == Constants.Brand);
string SportCodeValue, SportDescValue;
if (sport == null)
{
SportCodeValue = "";
SportDescValue = "";
}
else
{
SportCodeValue = sport.LookupValue.ToString();
SportDescValue = sport.description;
}
string GenderCodeValue, GenderCodeDesc;
if (gender == null)
{
GenderCodeValue = "";
GenderCodeDesc = "";
}
else
{
GenderCodeValue = gender.LookupValue.ToString();
GenderCodeDesc = gender.description;
}
string ColourCodeValue, ColourCodeDesc;
if (colour == null)
{
ColourCodeValue = "";
ColourCodeDesc = "";
}
else
{
ColourCodeValue = colour.LookupValue.ToString();
ColourCodeDesc = colour.description;
}
string SizeCodeValue, SizeCodeDesc;
if (Size == null)
{
SizeCodeValue = "";
SizeCodeDesc = "";
}
else
{
SizeCodeValue = Size.LookupValue.ToString();
SizeCodeDesc = Size.description;
}
string CategoryCodeValue, CategoryCodeDesc;
if (categorycode == null)
{
CategoryCodeValue = "";
CategoryCodeDesc = "";
}
else
{
CategoryCodeValue = categorycode.LookupValue.ToString();
CategoryCodeDesc = categorydesc.description;
}
string subcategorycodevalue, subcategorycodedesc;
if (categorycode == null)
{
subcategorycodevalue = "";
subcategorycodedesc = "";
}
else
{
subcategorycodevalue = subcategorycode.LookupValue.ToString();
subcategorycodedesc = subcategorydesc.description;
}
string brandcodecodevalue, brandcodecodedesc;
if (brandcode == null)
{
brandcodecodevalue = "";
brandcodecodedesc = "";
}
else
{
brandcodecodevalue = brandcode.LookupValue.ToString();
brandcodecodedesc = brandcode.description;
}
retList.Add(new TradeItemsExport()
{
ItemCode = item.ItemCode,
BarCode = item.BarCode,
Description = item.Description,
SupplierCode = item.SupplierCode,
SupplierStockCode = item.SupplierStockCode,
Product_Group_Code = "",
Product_Group_Desc = "",
SportCode = SportCodeValue,
SportDesc = SportDescValue,
GenderCode = GenderCodeValue,
GenderDesc = GenderCodeDesc,
ColourCode = ColourCodeValue,
ColourDesc = ColourCodeDesc,
SizeCode = SizeCodeValue,
SizeDesc = SizeCodeDesc,
CategoryCode = CategoryCodeValue,
CategoryDesc = CategoryCodeDesc,
subcategorycode = subcategorycodevalue,
subcategorydesc = subcategorycodedesc,
BrandsCode = brandcodecodevalue,
BrandsDesc = brandcodecodedesc,
Vat = item.Vat,
GrossWeight = item.Weight,
CommodityCode = item.CommodityCode,
price_exVAT = item.price_exVAT,
price_incVAT = item.price_incVAT,
currentprice_exVAT = item.currentprice_exVAT,
currentprice_incVAT = item.currentprice_incVAT,
creation_date = item.creation_date,
Inactive_date = item.Inactive_date,
status = 1
});
Console.Write(String.Format("Exporting stock item {0} with a current record of {1} of {2} \n", item.ItemCode.ToString(), count.ToString(), tradeItem.Count.ToString()));
EFStockItemExported _newStockitemImported = new EFStockItemExported();
_newStockitemImported.StockItemID = item.ItemCode;
_newStockitemImported.IsProcessed = true;
_newStockitemImported.DateImported = DateTime.Now;
db.EFStockItemExporteds.Add(_newStockitemImported);
db.SaveChanges();
}
else
{
Console.Write(string.Format("Stock Items to Process [{0}] check the table and remove entry if wish to re process.", 0));
}
}
}
}
}
catch (Exception ex)
{
}
return retList;
}
My problem that this takes around 30 mins to compute the results which is very slow.
This is the sql that I am doing which is a view that is the tradeitem that I am passing.
SELECT
dbo.PLSupplierAccount.SupplierAccountNumber, dbo.PLSupplierAccount.PLSupplierAccountID, dbo.PLSupplierAccount.SupplierAccountName,
dbo.PLSupplierAccount.SYSCurrencyID, dbo.PLSupplierAccount.MainTelephoneAreaCode, dbo.PLSupplierAccount.MainTelephoneCountryCode,
dbo.PLSupplierAccount.MainTelephoneSubscriberNumber, dbo.PLSupplierAccount.MainFaxCountryCode, dbo.PLSupplierAccount.MainFaxSubscriberNumber,
dbo.PLSupplierAccount.MainFaxAreaCode, dbo.PLSupplierContact.ContactName, dbo.PLSupplierContact.Description, dbo.PLSupplierContact.FirstName,
dbo.PLSupplierContact.MiddleName, dbo.PLSupplierContact.LastName, loc.AddressLine1, loc.AddressLine2, loc.AddressLine3, loc.AddressLine4, loc.PostCode,
loc.City, loc.County,
CAST(CASE WHEN loc.Country = 'Ireland' THEN 'IRL'
WHEN loc.Country = 'Great Britain'
THEN 'GBR'
ELSE 'ERR'
END AS nvarchar(3)) AS Country,
dbo.SYSCurrency.SYSCurrencyISOCodeID, dbo.SYSCurrency.SYSExchangeRateTypeID, dbo.SYSCurrency.Name AS CurrencyDescription,
dbo.SYSCurrency.Symbol AS CurrencySymbol
FROM
dbo.PLSupplierAccount
INNER JOIN
dbo.PLSupplierContact ON dbo.PLSupplierAccount.PLSupplierAccountID = dbo.PLSupplierContact.PLSupplierAccountID
INNER JOIN
dbo.PLSupplierLocation AS loc ON dbo.PLSupplierAccount.PLSupplierAccountID = loc.PLSupplierAccountID
AND dbo.PLSupplierContact.PLSupplierLocationID = loc.PLSupplierLocationID
INNER JOIN
dbo.SYSCurrency ON dbo.PLSupplierAccount.SYSCurrencyID = dbo.SYSCurrency.SYSCurrencyID
My quesiton is how would I change the above to include a sub query that would do the same as this function is doing above.
The query for the codes look up which is again another view is below.
SELECT
dbo.StockItem.ItemID, dbo.StockItem.Code, dbo.StockItem.Name, dbo.StockItemSearchCatVal.SearchValueID, dbo.SearchValue.Name AS Expr1,
dbo.SearchCategory.Name AS Expr2
FROM
dbo.SearchCategory
INNER JOIN
dbo.SearchValue ON dbo.SearchCategory.SearchCategoryID = dbo.SearchValue.SearchCategoryID
INNER JOIN
dbo.StockItemSearchCatVal ON dbo.SearchCategory.SearchCategoryID = dbo.StockItemSearchCatVal.SearchCategoryID
AND dbo.SearchValue.SearchValueID = dbo.StockItemSearchCatVal.SearchValueID
INNER JOIN
dbo.StockItem ON dbo.StockItemSearchCatVal.ItemID = dbo.StockItem.ItemID
I just feel I would get more benefit changing this into a subquery so that I am just returning the results to .net I am using the filehelpers library to output the results set of MapTradeItems to csv so obv more things I can do the better on the server.
Obv I would need some kind of temporary table to loop through the results but how quick would that be in sql server compared to a .net for each loop.
This is the poco class which I have to reproduce to csv.
[DelimitedRecord(",")]
public class TradeItemsExport
{
[FieldOrder(1)]
public string ItemCode { get; set; }
[FieldOrder(2)]
public string BarCode { get; set; }
[FieldOrder(3)]
public string Description { get; set; }
[FieldOrder(4)]
public string SupplierCode { get; set; }
[FieldOrder(5)]
public string SupplierStockCode { get; set; }
[FieldOrder(6)]
public string Product_Group_Code { get; set; }
[FieldOrder(7)]
public string Product_Group_Desc { get; set; }
[FieldOrder(8)]
public string SportCode { get; set; }
[FieldOrder(9)]
public string SportDesc { get; set; }
[FieldOrder(10)]
public string GenderCode { get; set; }
[FieldOrder(11)]
public string GenderDesc { get; set; }
[FieldOrder(12)]
public string ColourCode { get; set; }
[FieldOrder(13)]
public string ColourDesc { get; set; }
[FieldOrder(14)]
public string SizeCode { get; set; }
[FieldOrder(15)]
public string SizeDesc { get; set; }
[FieldOrder(16)]
public string CategoryCode { get; set; }
[FieldOrder(17)]
public string CategoryDesc { get; set; }
[FieldOrder(18)]
public string subcategorycode { get; set; }
[FieldOrder(19)]
public string subcategorydesc { get; set; }
[FieldOrder(20)]
public string BrandsCode { get; set; }
[FieldOrder(21)]
public string BrandsDesc { get; set; }
[FieldOrder(22)]
public Nullable<short> Vat { get; set; }
[FieldOrder(23)]
public decimal GrossWeight { get; set; }
[FieldOrder(24)]
public string CommodityCode { get; set; }
[FieldOrder(25)]
public decimal price_exVAT { get; set; }
[FieldOrder(26)]
public Nullable<decimal> price_incVAT { get; set; }
[FieldOrder(27)]
public Nullable<decimal> currentprice_exVAT { get; set; }
[FieldOrder(28)]
public Nullable<decimal> currentprice_incVAT { get; set; }
[FieldOrder(29)]
public System.DateTime creation_date { get; set; }
[FieldOrder(30)]
public Nullable<System.DateTime> Inactive_date { get; set; }
[FieldOrder(31)]
public int status { get; set; }
}
There is a method to read data from file
public static void ReadData(out StudentMarks[] Students, out int amount)
{
amount = 0;
Students = new StudentMarks[Max];
using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P3\\P3.2\\StudentsMarks.csv"))
{
reader.ReadLine(); reader.ReadLine();
string line = null;
int[] marks;
marks = new int[Max];
while (null != (line = reader.ReadLine()))
{
string[] values = line.Split(',');
string surname = values[0];
string name = values[1];
string group = values[2];
int amountOfMarks = int.Parse(values[3]);
int i = 0; int yMax = 3 + amountOfMarks;int yMin = 4;
while (amountOfMarks >= i)
{
if (yMin <= yMax)
{
marks[i] = int.Parse(values[yMin]);
yMin++;
}
i++;
}
StudentMarks MarksObj = new StudentMarks(surname, name, group, amountOfMarks, marks);
Students[amount++] = MarksObj;
}
}
}
There is a class:
class StudentMarks
{
public const int Max = 50;
public string Surname { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public int AmountOfMarks { get; set; }
public int[] Marks { get; set; }
public StudentMarks(string surname, string name, string group, int amountOfMarks, int[] marks)
{
Surname = surname;
Name = name;
Group = group;
AmountOfMarks = amountOfMarks;
Marks = marks;
}
}
The thing is, I cannot read "Marks", because I dont know how to initialize array in Class. My method should be working fine, I just can't put marks[i] into Students[i].Marks[y]
I don't understand why you want this.
but do you want like it?
class StudentMarks
{
public const int Max = 50;
public string Surname { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public int[] Marks { get; private set; }
public StudentMarks(string surname, string name, string group, int amountOfMarks)
{
Surname = surname;
Name = name;
Group = group;
Marks = new int[amountOfMarks];
}
}
public static void ReadData(out StudentMarks[] Students, out int amount)
{
amount = 0;
Students = new StudentMarks[Max];
using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P3\\P3.2\\StudentsMarks.csv"))
{
reader.ReadLine(); reader.ReadLine();
string line = null;
while (null != (line = reader.ReadLine()))
{
string[] values = line.Split(',');
string surname = values[0];
string name = values[1];
string group = values[2];
int i = 0; int yMax = 3 + amountOfMarks;int yMin = 4;
Students[amount] = new StudentMarks(surname, name, group, amountOfMark);
while (amountOfMarks >= i)
{
if (yMin <= yMax)
{
Students[amount].Marks[i] = int.Parse(values[yMin]);
yMin++;
}
i++;
}
amount++;
}
}
}
I have the following function which is accessible from one of my asp.net page:
/* QUERY TO RUN FROM ANY FUNCTION */
public void runQuery()
{
strMainSql = #"SELECT
CT.OBJECTID 'Object ID'
FROM HSI.RMOBJECTINSTANCE1224 CT
WHERE CT.ACTIVESTATUS = 0 AND CT.OBJECTID = '" + s + "'";
using (SqlConnection scConn = new SqlConnection(strConn))
{
scConn.Open();
using (SqlCommand scComm = new SqlCommand(strMainSql, scConn))
{
sdrRead = scComm.ExecuteReader();
while (sdrRead.Read())
{
/* CAN BE USED IN OTHER PAGES */
strTaskName = sdrRead[1].ToString();
strTaskDetail = sdrRead[2].ToString();
strTaskStartDate = sdrRead[3].ToString();
strIdentifier = sdrRead[4].ToString();
strStatus = sdrRead[5].ToString();
strDueDate = sdrRead[6].ToString();
strIssueDate = sdrRead[7].ToString();
strCompleted = sdrRead[8].ToString();
strNotes = sdrRead[9].ToString();
strProvider = sdrRead[10].ToString();
strService = sdrRead[11].ToString();
strCheckedDate = sdrRead[12].ToString();
strCheckedStatus = sdrRead[13].ToString();
strCheckedUser = sdrRead[14].ToString();
strClient = sdrRead[15].ToString();
hfMemoID.Value = sdrRead[16].ToString();
hfObjectID.Value = sdrRead[0].ToString();
break;
}
}
}
/* SPECIFIC TO THE PAGE ONLY */
lblUser.Text = strCheckedUser;
lblDateTime.Text = strCheckedDate;
lblTaskName.Text = strTaskName;
lblClient.Text = strClient;
lblID.Text = strIdentifier;
lblSvc.Text = strService;
lblProvider.Text = strProvider;
lblStat.Text = strStatus;
lblDueDate.Text = strDueDate;
lblDetail.Text = strTaskDetail;
lblTaskIssue.Text = strIssueDate;
lblStartDate.Text = strTaskStartDate;
lblCompleted.Text = strCompleted;
}
The question I have is, if I have to use the above function in multiple pages, instead of having a multiple copies of the same function which might lead to issue later on when updating, how do I make it into a class by itself so I can call it from any page and get the value from the SQL query?
What you can do is expose the results from the query as properties and then use the properties in the ASPX page.
using System.Data.SqlClient;
namespace MyNamespace
{
public class Task
{
public string strTaskName { get; set; }
public string strTaskDetail { get; set; }
public string strTaskStartDate { get; set; }
public string strIdentifier { get; set; }
public string strStatus { get; set; }
public string strDueDate { get; set; }
public string strIssueDate { get; set; }
public string strCompleted { get; set; }
public string strNotes { get; set; }
public string strProvider { get; set; }
public string strService { get; set; }
public string strCheckedDate { get; set; }
public string strCheckedStatus { get; set; }
public string strCheckedUser { get; set; }
public string strClient { get; set; }
// you need to define properties for the appropriate datatype on these
//hfMemoID
//hfObjectID
public string strConn { get; set; }
public void Load(string objectid)
{
var strMainSql = #"SELECT
CT.OBJECTID 'Object ID'
FROM HSI.RMOBJECTINSTANCE1224 CT
WHERE CT.ACTIVESTATUS = 0 AND CT.OBJECTID = '" + objectid + "'";
using (SqlConnection scConn = new SqlConnection(strConn))
{
scConn.Open();
using (SqlCommand scComm = new SqlCommand(strMainSql, scConn))
{
var sdrRead = scComm.ExecuteReader();
while (sdrRead.Read())
{
/* CAN BE USED IN OTHER PAGES */
this.strTaskName = sdrRead[1].ToString();
this.strTaskDetail = sdrRead[2].ToString();
this.strTaskStartDate = sdrRead[3].ToString();
this.strIdentifier = sdrRead[4].ToString();
this.strStatus = sdrRead[5].ToString();
this.strDueDate = sdrRead[6].ToString();
this.strIssueDate = sdrRead[7].ToString();
this.strCompleted = sdrRead[8].ToString();
this.strNotes = sdrRead[9].ToString();
this.strProvider = sdrRead[10].ToString();
this.strService = sdrRead[11].ToString();
this.strCheckedDate = sdrRead[12].ToString();
this.strCheckedStatus = sdrRead[13].ToString();
this.strCheckedUser = sdrRead[14].ToString();
this.strClient = sdrRead[15].ToString();
//
//hfMemoID.Value = sdrRead[16].ToString();
//hfObjectID.Value = sdrRead[0].ToString();
break;
}
}
}
}
}
}
In the code behind use the class to load the data and then set the controls using the properties
private MyNamespace.Task Task = new MyNamespace.Task();
protected void Page_Load(object sender, EventArgs e)
{
Task.strConn = "my connection string.";
Task.Load("task id to load");
// set the value into the controls.
lblUser.Text = Task.strCheckedUser;
lblDateTime.Text = Task.strCheckedDate;
lblTaskName.Text = Task.strTaskName;
lblClient.Text = Task.strClient;
lblID.Text = Task.strIdentifier;
lblSvc.Text = Task.strService;
lblProvider.Text = Task.strProvider;
lblStat.Text = Task.strStatus;
lblDueDate.Text = Task.strDueDate;
lblDetail.Text = Task.strTaskDetail;
lblTaskIssue.Text = Task.strIssueDate;
lblStartDate.Text = Task.strTaskStartDate;
lblCompleted.Text = Task.strCompleted;
}
I am having trouble with the representation of a date in JSON. I am using Service Stack as a web service to get the data from. My code on the server side is as follows:
public object Execute(GetNoPatientList request)
{
NoPatientList _noPatientList = new NoPatientList();
List<string> _noMatchPatientList = new List<string>();
List<NoPatientList> _newList = new List<NoPatientList>();
try
{
using (SqlConnection cn = new SqlConnection(Database.WaldenWebConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = "select [DateTimeStamp] as DateCreated,[ID],[PatientMRN],[FirstName],[MiddleName]"
+ " ,[LastName],convert(varchar,[DOB],101) as DOB,[Sex],[Note],[Source] as Interface"
+ " from PatientNoMatch"
+ " where FoundMatch = 'F'"
+ " and Show = 'T'"
+ " order by DateTimeStamp desc";
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
NoPatientList _noPatientList1 = new NoPatientList();
_noPatientList1.PatientMRN = dr["PatientMRN"].ToString();
_noPatientList1.FirstName = dr["FirstName"].ToString();
_noPatientList1.MiddleName = dr["MiddleName"].ToString();
_noPatientList1.LastName = dr["LastName"].ToString();
_noPatientList1.DOB = dr["DOB"].ToString();
_noPatientList1.Sex = dr["Sex"].ToString();
_noPatientList1.Note = dr["Note"].ToString();
_noPatientList1.DateCreated = dr.GetDateTime(0);
_noPatientList1.Interface = dr["Interface"].ToString();
_newList.Add(_noPatientList1);
}
return _newList;
}
}
}
catch
{
return _newList;
}
}
The type is represented as follows:
[DataContract]
public class NoPatientList
{
[DataMember]
public string ID { get; set; }
[DataMember]
public string PatientMRN { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string MiddleName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string Sex { get; set; }
[DataMember]
public string DOB { get; set; }
[DataMember]
public string Note { get; set; }
[DataMember]
public DateTime DateCreated { get; set; }
[DataMember]
public string Interface { get; set; }
}
The web service is being consumed by a Silverlight application from the following call:
/InterfaceUtility/servicestack/json/syncreply/
The Silverlight application is processing the code into a grid using the following code
private void GetNoPatientMatchData()
{
try
{
gridViewNoMatch.ItemsSource = null;
}
catch { }
_client = new WebClient();
_client.OpenReadCompleted += (a, f) =>
{
if (!f.Cancelled && f.Error == null)
{
_listOfNoPatientsMatches = new List<NoPatientList>();
MemoryStream _memoryStream = new MemoryStream();
f.Result.CopyTo(_memoryStream);
_memoryStream.Position = 0;
StreamReader _streamReader = new StreamReader(_memoryStream);
string _memoryStreamToText = _streamReader.ReadToEnd();
List<NoPatientList> _deserializedNoPatientList = (List<NoPatientList>)Newtonsoft.Json.JsonConvert.DeserializeObject(_memoryStreamToText, typeof(List<NoPatientList>));
gridViewNoMatch.ItemsSource = _deserializedNoPatientList;
}
else
{
MessageBox.Show(f.Error.Message,
"Error", MessageBoxButton.OK);
}
};
_client.OpenReadAsync(new Uri(_serviceUri + "getnopatientlist"));
The issue is that the times on DateTime field appear to always 6 hours off.
Any ideas as to what is going on?
This is probably a time zone issue. Check that:
Your webservice is returning you dates/times in UTC format.
Your code is parsing these dates/times as UTC dates and times.