Memory problems parsing HTML from site - c#
I have a infinate loop reading HTML from a site.
I tried parsing it using AgilityPack, but my machine slowed down over time and I had to stop the program when Task Manager showed it used 40 gigabytes.
So - I started parsing the HTML myself using split commands and indexof.
I put the data into a table and when the parsing is done I use a SqlBulkCopy to dump the data into the server.
I have tried both WriteToServer and WriteToServerAsync in the SqlBulkCopy.
I have also tried a wait(200) after each bulkcopy to give SQL Server some time.
Then I clear the table, clear the HTML string, the lists from the split and the loop starts over.
The program increases in size still. Not so fast as when using AgilityPack, but still I see running it for days will become a problem.
I want to mention that when I pause the program from Visual Studio, the size does not decrease. Telling me that giving SQL Server ( 2019 ) time does not help out.
Any ideas on why the program is increasing in size ? Or tips I can try to make it better?
UPDATE!
This is my infinate loop
CMCView is a WebView2
while (true)
{
string html = await CMCView.ExecuteScriptAsync("document.documentElement.outerHTML;");
string h = Regex.Replace(html,#"\u(?[a-zA-Z0-9]{4})",m => {return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();});
HTMLDecoder hTMLDecoder = new HTMLDecoder(h);
hTMLDecoder = null;
}
the HTMLDecoder is where i parse the h string and save to database.
The site has three windows I want to use : Indexes, Commodities and Currencies.
I find them by splitting the FeatureWindowController into lists of product-name-child.
To increase speed, I load the tables and put the ID and name in Dictionarys.
Then I loop the indexesRows, commodityRows and currencyRows.
I compare the name in the HTML with the name in the dictionary and add it if it is not there. I know - I will change it to parameters in the future, but I do not think that is the issue here.
In each of those loops I add to the History table. I clear it, clear the dictionary, load the table and load the dictionarys for each save.
And I add info to the History table.
After those three loops I use the SqlBulkCopy to save to database.
class HTMLDecoder
{
DataTable indekserDataTable = new DataTable();
DataTable commoditiesDataTable = new DataTable();
DataTable valutaDataTable = new DataTable();
DataTable countriesDataTable = new DataTable();
DataTable marketTypesDataTable = new DataTable();
DataTable productsDataTable = new DataTable();
DataTable historyDataTable = new DataTable();
Dictionary<string, int> countriesDictionary = new Dictionary<string, int>();
Dictionary<string, int> marketTypesDictionary = new Dictionary<string, int>();
Dictionary<string, int> productsDictionary = new Dictionary<string, int>();
public HTMLDecoder(string HTML)
{
HTMLDecoder_(HTML);
HTML = string.Empty;
}
public async Task<string> HTMLDecoder_(string HTML)
{
Stopwatch stopwatchstopwatch = new Stopwatch();
Stopwatch stopwatchstopwatch1 = new Stopwatch();
Task returnTask = null;
indekserDataTable = LoadTable("SELECT * FROM PRODUCTS WHERE ISINDEX = 1");
commoditiesDataTable = LoadTable("SELECT * FROM PRODUCTS WHERE ISCOMMODITY = 1");
valutaDataTable = LoadTable("SELECT * FROM PRODUCTS WHERE ISCURRENCY= 1");
countriesDataTable = LoadTable("SELECT * FROM COUNTRIES");
marketTypesDataTable = LoadTable("SELECT * FROM MARKETTYPES");
productsDataTable = LoadTable("SELECT * FROM PRODUCTS");
historyDataTable = LoadTable("SELECT TOP(0) * FROM HISTORY");
setCountriesDictionary();
setMarketTypesDictionary();
setProductsDictionary();
List<string> spl1 = HTML.Split(new string[] { "FeatureWindowController" }, StringSplitOptions.RemoveEmptyEntries).ToList();
spl1.RemoveAt(0);
List<string> indexesRows2 = spl1[2].Split(new string[] { "product-name-child" }, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string> commoditiesRows2 = spl1[3].Split(new string[] { "product-name-child" }, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string> currencyRows2 = spl1[1].Split(new string[] { "product-name-child" }, StringSplitOptions.RemoveEmptyEntries).ToList();
spl1.Clear();
List<string> indexesRows = (from order in indexesRows2
where (order.IndexOf("title") < 30 &&
order.IndexOf("title") > -1)
select order).ToList();
List<string> commoditiesRows = (from order in commoditiesRows2
where (order.IndexOf("title") < 30 &&
order.IndexOf("title") > -1)
select order).ToList();
List<string> currencyRows = (from order in currencyRows2
where (order.IndexOf("title") < 30 &&
order.IndexOf("title") > -1)
select order).ToList();
#region indekser
int counter = 0;
foreach (string ups2 in indexesRows)
{
counter++;
bool error = false;
DataRow newHistoryRow = historyDataTable.NewRow();
newHistoryRow["DATE"] = DateTime.Now;
newHistoryRow["TIME"] = DateTime.Now.TimeOfDay;
string ups = ups2.Replace("\\", "");
List<string> list = ups.Split(new string[] { "title=" }, StringSplitOptions.RemoveEmptyEntries).ToList();
string markettype = list[2].Substring(0, list[2].IndexOf("\"", 1)).Replace("\"", "");
int marketTypeId = 0;
bool marketTypeExists = marketTypesDictionary.ContainsKey(markettype);
if (!marketTypeExists)
{
string sqlQuery = "INSERT INTO MARKETTYPES (MarketTypeName) VALUES ('" + markettype + "')";
ExecuteSQLNoResult(sqlQuery);
marketTypesDataTable.Rows.Clear();
marketTypesDataTable = LoadTable("SELECT * FROM MARKETTYPES");
setMarketTypesDictionary();
}
marketTypesDictionary.TryGetValue(markettype, out marketTypeId);
string productName = list[1].Substring(0, list[1].IndexOf("\"", 1)).Replace("\"", "");
int productId = 0;
bool productExists = productsDictionary.ContainsKey(productName);
if (!productExists)
{
string sqlQuery = "INSERT INTO PRODUCTS (PRODUCTNAME, MARKETTYPEID, ISINDEX) VALUES ('" + productName + "', " + marketTypeId + ", 1)";
ExecuteSQLNoResult(sqlQuery);
productsDataTable.Rows.Clear();
productsDataTable = LoadTable("SELECT * FROM PRODUCTS");
setProductsDictionary();
}
productsDictionary.TryGetValue(productName, out productId);
newHistoryRow["PRODUCTID"] = productId;
List<string> list2 = list[2].Split(new string[] { "span class=\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
int countryStart = list2[2].IndexOf(">") + 1;
int countryEnd = list2[2].IndexOf("<");
string countryName = list2[2].Substring(countryStart, countryEnd - countryStart);
#region insert Country if not Exists
if (countryName.ToUpper() != "GLOBAL")
{
bool countryExists = countriesDictionary.ContainsKey(countryName);
if (!countryExists)
{
int isDevelopedMarket = 0;
int isEmergingMarket = 0;
if (markettype == "Utviklede markeder")
isDevelopedMarket = 1;
else
isEmergingMarket = 1;
string sqlQuery = "INSERT INTO COUNTRIES (COUNTRYNAME, isDevelopedMarket, isEmergingMarket) VALUES ('" + countryName + "', " + isDevelopedMarket + ", " + isEmergingMarket + ")";
ExecuteSQLNoResult(sqlQuery);
countriesDataTable.Rows.Clear();
countriesDataTable = LoadTable("SELECT * FROM COUNTRIES");
setCountriesDictionary();
}
}
#endregion
int percentageStart = list2[3].IndexOf(">") + 1;
string percentage = list2[3].Substring(percentageStart, list2[3].IndexOf("<") - percentageStart).Replace(",", ".").Replace(" ", "").Replace("%", "").Trim();
int numericStart = list2[4].IndexOf(">") + 1;
string numeric = list2[4].Substring(numericStart, (list2[4].IndexOf("<") - numericStart)).Replace(",", ".").Replace(" ", "");
if (numeric != "-")
{
try
{
newHistoryRow["ChangeInPercent"] = percentage;
newHistoryRow["ChangeValue"] = numeric;
}
catch
{
error = true;
}
}
list2.Clear();
string low = list[3].Substring(0, list[3].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string high = list[4].Substring(0, list[4].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string open = list[5].Substring(0, list[5].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string close = list[6].Substring(0, list[6].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
List<string> string7 = list[7].Split(new string[] { "price-panel-button__label\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
string sell = string7[1].Substring(1, string7[2].IndexOf("<") - 1).Replace(" ", "").Replace(",", ".");
string buy = string7[2].Substring(1, string7[2].IndexOf("<") - 1).Replace(" ", "").Replace(",", ".");
list.Clear();
string7.Clear();
if (low != "-")
{
try
{
newHistoryRow["LOW"] = low;
newHistoryRow["HIGH"] = high;
newHistoryRow["OPEN"] = open;
newHistoryRow["CLOSE"] = close;
newHistoryRow["SELL"] = sell;
newHistoryRow["BUY"] = buy;
}
catch
{
error = true;
}
}
if (!error)
{
if (newHistoryRow[4].ToString().Length > 0)
{
historyDataTable.Rows.Add(newHistoryRow);
newHistoryRow = historyDataTable.NewRow();
}
}
}
indexesRows.Clear();
foreach (string ups2 in commoditiesRows)
{
counter++;
bool error = false;
DataRow newHistoryRow = historyDataTable.NewRow();
newHistoryRow["DATE"] = DateTime.Now;
newHistoryRow["TIME"] = DateTime.Now.TimeOfDay;
string ups = ups2.Replace("\\", "");
List<string> list = ups.Split(new string[] { "title=" }, StringSplitOptions.RemoveEmptyEntries).ToList();
//List<string> list = ups.Split(new string[] { "title=\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (list.Count < 2)
{
continue;
}
string markettype = list[2].Substring(0, list[2].IndexOf("\"", 1)).Replace("\"", "");
int marketTypeId = 0;
bool marketTypeExists = marketTypesDictionary.ContainsKey(markettype);
if (!marketTypeExists)
{
string sqlQuery = "INSERT INTO MARKETTYPES (MarketTypeName) VALUES ('" + markettype + "')";
ExecuteSQLNoResult(sqlQuery);
marketTypesDataTable.Rows.Clear();
marketTypesDataTable = LoadTable("SELECT * FROM MARKETTYPES");
setMarketTypesDictionary();
}
marketTypesDictionary.TryGetValue(markettype, out marketTypeId);
string productName = list[1].Substring(0, list[1].IndexOf("\"", 1)).Replace("\"", "");
int productId = 0;
bool productExists = productsDictionary.ContainsKey(productName);
if (!productExists)
{
string sqlQuery = "INSERT INTO PRODUCTS (PRODUCTNAME, MARKETTYPEID, ISINDEX) VALUES ('" + productName + "', " + marketTypeId + ", 1)";
ExecuteSQLNoResult(sqlQuery);
productsDataTable.Rows.Clear();
productsDataTable = LoadTable("SELECT * FROM PRODUCTS");
setProductsDictionary();
}
productsDictionary.TryGetValue(productName, out productId);
newHistoryRow["PRODUCTID"] = productId;
List<string> list2 = list[2].Split(new string[] { "span class=\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
int percentageStart = list2[3].IndexOf(">") + 1;
string percentage = list2[3].Substring(percentageStart, list2[3].IndexOf("<") - percentageStart).Replace(",", ".").Replace(" ", "").Replace("%", "").Trim();
int numericStart = list2[4].IndexOf(">") + 1;
string numeric = list2[4].Substring(numericStart, (list2[4].IndexOf("<") - numericStart)).Replace(",", ".").Replace(" ", "");
if (numeric != "-")
{
try
{
newHistoryRow["ChangeInPercent"] = percentage;
newHistoryRow["ChangeValue"] = numeric;
}
catch
{
error = true;
}
}
list2.Clear();
string low = list[3].Substring(0, list[3].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string high = list[4].Substring(0, list[4].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string open = list[5].Substring(0, list[5].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string close = list[6].Substring(0, list[6].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
List<string> string7 = list[7].Split(new string[] { "price-panel-button__label\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
string sell = string7[1].Substring(1, string7[2].IndexOf("<") - 1).Replace(" ", "").Replace(",", ".");
string buy = string7[2].Substring(1, string7[2].IndexOf("<") - 1).Replace(" ", "").Replace(",", ".");
list.Clear();
string7.Clear();
if (low != "-")
{
try
{
newHistoryRow["LOW"] = low;
newHistoryRow["HIGH"] = high;
newHistoryRow["OPEN"] = open;
newHistoryRow["CLOSE"] = close;
newHistoryRow["SELL"] = sell;
newHistoryRow["BUY"] = buy;
}
catch
{
error = true;
}
}
if (!error)
{
if (newHistoryRow[4].ToString().Length > 0)
{
historyDataTable.Rows.Add(newHistoryRow);
newHistoryRow = historyDataTable.NewRow();
}
}
}
commoditiesRows.Clear();
foreach (string ups2 in currencyRows)
{
counter++;
bool error = false;
DataRow newHistoryRow = historyDataTable.NewRow();
newHistoryRow["DATE"] = DateTime.Now;
newHistoryRow["TIME"] = DateTime.Now.TimeOfDay;
string ups = ups2.Replace("\\", "");
List<string> list = ups.Split(new string[] { "title=" }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (list.Count < 2)
{
continue;
}
string markettype = list[2].Substring(0, list[2].IndexOf("\"", 1)).Replace("\"", "");
int marketTypeId = 0;
bool marketTypeExists = marketTypesDictionary.ContainsKey(markettype);
if (!marketTypeExists)
{
string sqlQuery = "INSERT INTO MARKETTYPES (MarketTypeName) VALUES ('" + markettype + "')";
ExecuteSQLNoResult(sqlQuery);
marketTypesDataTable.Rows.Clear();
marketTypesDataTable = LoadTable("SELECT * FROM MARKETTYPES");
setMarketTypesDictionary();
}
marketTypesDictionary.TryGetValue(markettype, out marketTypeId);
string productName = list[1].Substring(0, list[1].IndexOf("\"", 1)).Replace("\"", "");
int productId = 0;
bool productExists = productsDictionary.ContainsKey(productName);
if (!productExists)
{
string sqlQuery = "INSERT INTO PRODUCTS (PRODUCTNAME, MARKETTYPEID, ISINDEX) VALUES ('" + productName + "', " + marketTypeId + ", 1)";
ExecuteSQLNoResult(sqlQuery);
productsDataTable.Rows.Clear();
productsDataTable = LoadTable("SELECT * FROM PRODUCTS");
setProductsDictionary();
}
productsDictionary.TryGetValue(productName, out productId);
newHistoryRow["PRODUCTID"] = productId;
List<string> list2 = list[2].Split(new string[] { "span class=\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
int percentageStart = list2[3].IndexOf(">") + 1;
string percentage = list2[3].Substring(percentageStart, list2[3].IndexOf("<") - percentageStart).Replace(",", ".").Replace(" ", "").Replace("%", "").Trim();
if (percentage.Length < 2)
{
}
int numericStart = list2[4].IndexOf(">") + 1;
string numeric = list2[4].Substring(numericStart, (list2[4].IndexOf("<") - numericStart)).Replace(",", ".").Replace(" ", "");
if (numeric != "-")
{
try
{
newHistoryRow["ChangeInPercent"] = percentage;
newHistoryRow["ChangeValue"] = numeric;
}
catch
{
error = true;
}
}
list2.Clear();
string low = list[3].Substring(0, list[3].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string high = list[4].Substring(0, list[4].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string open = list[5].Substring(0, list[5].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
string close = list[6].Substring(0, list[6].IndexOf("\"", 1)).Replace(" ", "").Replace(",", ".").Replace("\"", "");
List<string> string7 = list[7].Split(new string[] { "price-panel-button__label\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
string sell = string7[1].Substring(1, string7[2].IndexOf("<") - 1).Replace(" ", "").Replace(",", ".");
string buy = string7[2].Substring(1, string7[2].IndexOf("<") - 1).Replace(" ", "").Replace(",", ".");
list.Clear();
string7.Clear();
if (low != "-")
{
try
{
newHistoryRow["LOW"] = low;
newHistoryRow["HIGH"] = high;
newHistoryRow["OPEN"] = open;
newHistoryRow["CLOSE"] = close;
newHistoryRow["SELL"] = sell;
newHistoryRow["BUY"] = buy;
}
catch
{
error = true;
}
}
if (!error)
{
if (newHistoryRow[4].ToString().Length > 0)
{
historyDataTable.Rows.Add(newHistoryRow);
newHistoryRow = historyDataTable.NewRow();
}
}
}
currencyRows.Clear();
stopwatchstopwatch.Reset();
stopwatchstopwatch.Start();
using (SqlBulkCopy sqlbc = new SqlBulkCopy(SQLHelper.getSQLConnectionString()))
{
try
{
sqlbc.DestinationTableName = "HISTORY";
await (returnTask = sqlbc.WriteToServerAsync(historyDataTable));
}
catch (Exception e)
{
}
}
HTML = string.Empty;
stopwatchstopwatch.Stop();
historyDataTable.Rows.Clear();
#endregion
return returnTask.ToString();
}
public DataTable LoadTable(string SQLStatement)
{
var table = new DataTable();
//Server = HAAKON; Database =SignalTrader; Trusted_Connection=True ;Trust Server Certificate=true Server = HAAKON
using (var da = new SqlDataAdapter(SQLStatement, SQLHelper.getSQLConnectionString()))
{
da.Fill(table);
}
return table;
}
public void ExecuteSQLNoResult(string SQLStatement)
{
using (SqlConnection conn = new(SQLHelper.getSQLConnectionString()))
{
using (SqlCommand command = new SqlCommand(SQLStatement, conn))
{
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
}
private void setCountriesDictionary()
{
countriesDictionary.Clear();
countriesDictionary = countriesDataTable.AsEnumerable().ToDictionary<DataRow, string, int>(row => row.Field<string>(1), row => row.Field<int>(0));
}
private void setMarketTypesDictionary()
{
marketTypesDictionary.Clear();
marketTypesDictionary = marketTypesDataTable.AsEnumerable().ToDictionary<DataRow, string, int>(row => row.Field<string>(1), row => row.Field<int>(0));
}
private void setProductsDictionary()
{
productsDictionary.Clear();
productsDictionary = productsDataTable.AsEnumerable().ToDictionary<DataRow, string, int>(row => row.Field<string>(1), row => row.Field<int>(0));
}
}
Related
Visual Studio Database Not Updating
I am working on a school project and for some reason my mysql database doesn't update despite no of row changed is more than 0 and triggering the Update sucessful alert. It also manage to only update my image data from my fileupload. **admin_products_details_edit.aspx.cs** protected void btn_ProdEdit_Click(object sender, EventArgs e) { int result = 0; string image = ""; if (FileUpload_ProdImg.HasFile == true) { image = "images\\" + FileUpload_ProdImg.FileName; img_result.ImageUrl = FileUpload_ProdImg.FileName; } else { image = img_result.ImageUrl; } Product Prod = new Product(); string datProdID = lbl_ProdID.Text; string datProdName = tb_ProdName.Text; string datProdDesc = tb_ProdDesc.Text; string datProdImg = img_result.ImageUrl; decimal datProdPrice = decimal.Parse(tb_ProdPrice.Text); int datProdCal = int.Parse(tb_ProdCal.Text); int datStockLvl = int.Parse(tb_StockLvl.Text); result = Prod.ProductUpdate(datProdID, datProdName, datProdDesc, datProdImg, datProdPrice, datProdCal, datStockLvl); if (result > 0) { string saveimg = Server.MapPath(" ") + "\\" + image; FileUpload_ProdImg.SaveAs(saveimg); Response.Write("<script>alert('Update successful');</script>"); Response.Redirect("admin_products_details.aspx?ProdID=" + datProdID); } else { Response.Write("<script>alert('Update fail');</script>"); } }<-The code for the button edit event trigger **Product.cs** ...public int ProductUpdate(string upID, string upName, string upDesc, string upImg, decimal upPrice, int upCal, int upstkLvl) { string queryStr = "UPDATE Products SET" + " ProdName = #productName, " + " ProdDesc = #productDesc, " + " ProdImg = #productImage, " + " ProdPrice = #productPrice, " + " ProdCalorie = #productCal, " + " StockLevel = #productStkLvl " + " WHERE ProdID = #productID"; SqlConnection conn = new SqlConnection(_connStr); SqlCommand cmd = new SqlCommand(queryStr, conn); cmd.Parameters.AddWithValue("#productID", upID); cmd.Parameters.AddWithValue("#productName", upName); cmd.Parameters.AddWithValue("#productDesc", upDesc); cmd.Parameters.AddWithValue("#productImage", upImg); cmd.Parameters.AddWithValue("#productPrice", upPrice); cmd.Parameters.AddWithValue("#productCal", upCal); cmd.Parameters.AddWithValue("#productStkLvl", upstkLvl); conn.Open(); int nofRow = 0; nofRow = cmd.ExecuteNonQuery(); conn.Close(); return nofRow; }<-The code for updating the mysql database,located in a different cs file,titled Product.cs My mysql database table is called Products Thank you very much for your help in advance.
There is no row at position 2 C# Runtime Exception
I am retrieving data from db and displaying in Label. but the problem is that this cannot retrieve db tabel first row data. Its print Second row data. If i given the db first row c_code then it is given the Error "There is no row at position 2" Kindly please solve my problem. Thanks you private void Get_Purchasing_Amount() { try { string get_P_Amount = ""; double var_P_Amount = 0; int var_C_Code = 0; string query = "select c_code as 'code' from `db_vegetable`.`tbl_payment_master`"; DataTable dt_C_Code = method_Class.method_Class.FetchRecords(query); if (dt_C_Code.Rows.Count > 0) { for (int i = 0; i <= dt_C_Code.Rows.Count; i++) { var_C_Code = Convert.ToInt32(dt_C_Code.Rows[i]["code"]); if (var_C_Code.Equals(Convert.ToInt32(txt_Customer_Code.Text))) { if (check_All.Checked.Equals(true)) { get_P_Amount = "SELECT IFNULL(`purchasing`,0) AS 'purchasing' FROM `db_vegetable`.`tbl_payment_master` WHERE `c_code` = " + txt_Customer_Code.Text + ""; } else { string dt_Query = "select `id` as 'id' from `db_vegetable`.`tbl_order_details`"; DataTable dt_C_O = method_Class.method_Class.FetchRecords(dt_Query); if (dt_C_O.Rows.Count > 0) get_P_Amount = "SELECT IFNULL(SUM(t_price),0) as 'purchasing' FROM `db_vegetable`.`tbl_order_details` WHERE `c_code` = " + txt_Customer_Code.Text + " AND (`date` BETWEEN '" + txt_From_Date.Text + "' AND '" + txt_To_Date.Text + "')"; else lbl_Purchasing_Amount.Text = "0"; } DataTable dt = method_Class.method_Class.FetchRecords(get_P_Amount); var_P_Amount = Convert.ToDouble(dt.Rows[0]["purchasing"]); lbl_Purchasing_Amount.Text = var_P_Amount.ToString(); } else { lbl_Purchasing_Amount.Text = "0"; } } } else { lbl_Purchasing_Amount.Text = "0"; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
I believe this is probably the issue: for (int i = 0; i <= dt_C_Code.Rows.Count; ; i++) {...} Please consider substituting foreach (DataRow row in dt_C_Code.Rows) { ...} If it's important which row should logically come "first", then please consider using an order by clause in your SQL statement.
Now problem is solve the problem is that break key word. private void Get_Purchasing_Amount() { try { string get_P_Amount = ""; double var_P_Amount = 0; //int var_C_Code = 0; string query = "select c_code as 'code' from `db_vegetable`.`tbl_payment_master`"; DataTable dt_C_Code = method_Class.method_Class.FetchRecords(query); if (dt_C_Code.Rows.Count > 0) { for (int i = 0; i <= dt_C_Code.Rows.Count; i++)//these line generate error please check this { //var_C_Code = Convert.ToInt32(dt_C_Code.Rows[i]["code"]); if (Convert.ToInt32(dt_C_Code.Rows[i]["code"]).Equals(Convert.ToInt32(txt_Customer_Code.Text))) { if (check_All.Checked.Equals(true)) { get_P_Amount = "SELECT IFNULL(`purchasing`,0) AS 'purchasing' FROM `db_vegetable`.`tbl_payment_master` WHERE `c_code` = " + txt_Customer_Code.Text + ""; } else { string dt_Query = "select `id` as 'id' from `db_vegetable`.`tbl_order_details`"; DataTable dt_C_O = method_Class.method_Class.FetchRecords(dt_Query); if (dt_C_O.Rows.Count > 0) get_P_Amount = "SELECT IFNULL(SUM(t_price),0) as 'purchasing' FROM `db_vegetable`.`tbl_order_details` WHERE `c_code` = " + txt_Customer_Code.Text + " AND (`date` BETWEEN '" + txt_From_Date.Text + "' AND '" + txt_To_Date.Text + "')"; else lbl_Purchasing_Amount.Text = "0"; } DataTable dt = method_Class.method_Class.FetchRecords(get_P_Amount); var_P_Amount = Convert.ToDouble(dt.Rows[0]["purchasing"]); lbl_Purchasing_Amount.Text = var_P_Amount.ToString(); break; } else { lbl_Purchasing_Amount.Text = "0"; } } } else { lbl_Purchasing_Amount.Text = "0"; } } catch (Exception) { } }
GridView - Line break inside Cell
I've been trying to get a line break between this inside every cell in column 1: Value 1, Value 2, Value 3 However the line breaks are just showing up as plain text in the cell: <asp:GridView ID="dg1" runat="server" AllowSorting="true" OnSorting="Sort" AutoGenerateColumns="false" OnSelectedIndexChanged="dg1_SelectedIndexChanged" CssClass="DGgeneral" HeaderStyle-CssClass="DGheader" RowStyle-CssClass="DGrow1" AlternatingRowStyle-CssClass="DGrow2" > <Columns> </Columns> </asp:GridView> I am doing everything programatically so I can't add HTMLEncode="false" to BoundFields which is a huge pain. public void SearchQuery(string sOrderBy, string sOrderByColumn) { //Create and Clear Datatable and Gridview dt.Clear(); dg1.DataSource = null; dg1.DataBind(); //Initialise Strings string sTLTaskID = ""; string sTitle = ""; string sForename = ""; string sSurname = ""; string sEmail = ""; string sMobile = ""; string sHome = ""; string sDateTime = ""; string sState = ""; string sFormName = ""; //Initialise Column Names dt.Columns.Add("Id/Form/State"); dt.Columns.Add("Title"); dt.Columns.Add("Forename"); dt.Columns.Add("Surname"); dt.Columns.Add("Email address"); dt.Columns.Add("Telephone 1"); dt.Columns.Add("Telephone 2"); dt.Columns.Add("Date & Time"); //Connect to ******** using (var connection = new SqlConnection( "Data Source = ******;" + "Integrated Security = ******;" + "persist security info = ******;" + "Initial Catalog = ********;")) { //Establish SQL Command using (var command = connection.CreateCommand()) { command.CommandText = "********"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("#TLTaskID", txtTLTaskID.Text); command.Parameters.AddWithValue("#Name", txtName.Text); command.Parameters.AddWithValue("#Email", txtEmail.Text); command.Parameters.AddWithValue("#MNumber", txtNumber.Text); command.Parameters.AddWithValue("#HNumber", txtNumber.Text); if ((sOrderBy == null) || (sOrderBy == "")) { command.Parameters.AddWithValue("#AscDesc", ""); } else if (sOrderBy == "Asc") { command.Parameters.AddWithValue("#AscDesc", "Asc"); } else if (sOrderBy == "Desc") { command.Parameters.AddWithValue("#AscDesc", "Desc"); } if ((sOrderByColumn == null) || (sOrderByColumn == "")) { command.Parameters.AddWithValue("#OrderByColumn", ""); } else { command.Parameters.AddWithValue("#OrderByColumn", sOrderByColumn); } connection.Open(); //Initialise Column Names using (var reader = command.ExecuteReader()) { var column1 = reader.GetOrdinal("TLTaskID"); var column2 = reader.GetOrdinal("Title"); var column3 = reader.GetOrdinal("FirstName"); var column4 = reader.GetOrdinal("Surname"); var column5 = reader.GetOrdinal("Email"); var column6 = reader.GetOrdinal("Telephone1"); var column7 = reader.GetOrdinal("Telephone2"); var column8 = reader.GetOrdinal("DateTime"); var column9 = reader.GetOrdinal("State"); var column10 = reader.GetOrdinal("FormName"); //Loop until out of Rows in table while (reader.Read()) { //Set values of each row to a variable var TLTaskID = reader.GetValue(column1); var Title = reader.GetValue(column2); var Forename = reader.GetValue(column3); var Surname = reader.GetValue(column4); var Email = reader.GetValue(column5); var Mobile = reader.GetValue(column6); var Home = reader.GetValue(column7); var DateTime = reader.GetValue(column8); var State = reader.GetValue(column9); var FormName = reader.GetValue(column10); //Convert to strings //Possible speed up **** sTLTaskID = TLTaskID.ToString(); sTitle = Title.ToString(); sForename = Forename.ToString(); sSurname = Surname.ToString(); sEmail = Email.ToString(); sMobile = Mobile.ToString(); sHome = Home.ToString(); sDateTime = DateTime.ToString(); sState = State.ToString(); sFormName = FormName.ToString(); //Add to DataTable var dr = dt.NewRow(); dr["Id/Form/State"] = sTLTaskID + ", " + sFormName + ", " + sState; dr["Title"] = sTitle; dr["Forename"] = sForename; dr["Surname"] = sSurname; dr["Email address"] = sEmail; dr["Telephone 1"] = sMobile; dr["Telephone 2"] = sHome; dr["Date & Time"] = sDateTime; dt.Rows.Add(dr); //Set GridView's datasource to Datatable dr dg1.DataSource = dt; dg1.DataBind(); } } } } int iRowCount = dt.Rows.Count; if (iRowCount > 0) { lblRecordTotal.Text = "Records: 1 - " + iRowCount.ToString() + " of " + iRowCount.ToString(); } else { lblRecordTotal.Text = "No records found."; } } If anything more is required please ask away. Any ideas?
In your RowDataBound event of GridView add below line: e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("\n", "<br/>"); Note: You have to add OnRowDataBound event to GridView like this: <asp:GridView ID="dg1" runat="server" OnRowDataBound="dg1_RowDataBound">
How can I make this a never ending loop with time out every 1 successful process
This is my code which parses a string coming from a database. The database has around 50 data strings and continuously grows every 5 seconds. I want this code to enter a never ending loop so if new data arrives and the table is empty it will immediately parse it. I'm only asking for possible suggestion on how to make this code a never ending looping processor. Code below private void btnActiveStart_Click(object sender, EventArgs e) { string connetionString = null; SqlConnection cnn; connetionString = "Data Source=SAMPLE12345;Initial Catalog=SAMPLE_TABLE;User ID=sa;Password=str0ngp#ssw0rd;MultipleActiveResultSets=True"; cnn = new SqlConnection(connetionString); cnn.Open(); string sqlquery = "SELECT top 1 SMSMSG,SMSDT FROM GPRSIN WHERE isHEX = '0'"; SqlCommand command = new SqlCommand(sqlquery, cnn); SqlDataReader sdr = command.ExecuteReader(); while (sdr.Read()) { string input = sdr["SMSMSG"].ToString(); string smsdt = sdr["SMSDT"].ToString(); int s = input.Length; if (s == 105) { string account = input.Substring(0, 15); txtbxImeiActive.Text = account; //ACCOUNT DateTime now = DateTime.Now; String finnow = now.ToString("yyyy-MM-dd HH:mm:ss.fff"); txtbxDateTimeStampFast.Text = finnow.ToString(); //DATETIME //Convert date START string datetime = input.Substring(65, 12); string hour = datetime.Substring(0, 2); string minutes = datetime.Substring(2, 2); string seconds = datetime.Substring(4, 2); string day = datetime.Substring(6, 2); string month = datetime.Substring(8, 2); string year = datetime.Substring(10, 2); string strdate = "20" + year + "-" + month + "-" + day + " "; string strtime = hour + ":" + minutes + ":" + seconds + ".000"; string findatetime = strdate + strtime; DateTime gpsdatefast = Convert.ToDateTime(strdate); String fingpsdatefast= gpsdatefast.ToString("yyyy-MM-dd HH:mm:ss.fff"); DateTime gpstimesfast = Convert.ToDateTime(strtime); String fingpstimefast = gpstimesfast.ToString("HH:mm:ss"); //Convert date END txtbxGpsDateFast.Text = fingpsdatefast.ToString(); //GPSDATE txtbxGpsTimeFast.Text = fingpstimefast.ToString(); //GPSTIME DateTime trxdatefast = Convert.ToDateTime(smsdt); String fintrxdatefast = trxdatefast.ToString("yyyy-MM-dd 00:00:00.000"); txtbxTrxDateFast.Text = fintrxdatefast; DateTime trxtimefast = Convert.ToDateTime(smsdt); String fintrxtimefast = trxtimefast.ToString("HH:mm:ss"); txtbxTrxTimeFast.Text = fintrxtimefast; DateTime philtime = Convert.ToDateTime(findatetime).AddHours(8); String finphiltime = philtime.ToString("yyyy-MM-dd HH:mm:ss.fff"); txtbxPhilTimeFast.Text= finphiltime; //Convert DMS format to decimal (Degrees and fragment minutes) START LONG string deglong = input.Substring(35, 10); string degreeslong = deglong.Substring(0, 3); decimal degfin2 = decimal.Parse(degreeslong); string minuteslong = deglong.Substring(3, 6); decimal minfin2 = decimal.Parse(minuteslong); decimal resminlong = minfin2 / 60; decimal longresult = degfin2 + resminlong; string gettlong = longresult.ToString(); string longresultcut = gettlong.Substring(0, 7); decimal tlongcut = decimal.Parse(longresultcut); //Convert DMS format to decimal (Degrees and fragment minutes) START LONG txtbxLongFast.Text = Math.Round(longresult, 6).ToString(); txtbxTlong.Text = tlongcut.ToString(); //Convert DMS format to decimal (Degrees and fragment minutes) START LAT string deglat = input.Substring(25, 9); string degreeslat = deglat.Substring(0, 2); decimal degfin1 = decimal.Parse(degreeslat); string minuteslat = deglat.Substring(2, 6); decimal minfin1 = decimal.Parse(minuteslat); decimal resminlat = minfin1 / 60; decimal latresult = degfin1 + resminlat; string gettlat = latresult.ToString(); string latresultcut = gettlat.Substring(0, 6); decimal tlatcut = decimal.Parse(latresultcut); //Convert DMS format to decimal (Degrees and fragment minutes) END LAT txtbxLatFast.Text = Math.Round(latresult, 6).ToString(); txtbxTlat.Text = tlatcut.ToString(); string tlocquery = "SELECT LAC,CID,LONG,LAT,STREET,MUNICIPAL FROM TRIANGULATION WHERE lat LIKE " + "'%" + tlatcut.ToString() + "%' AND long LIKE '%" + tlongcut.ToString() + "%'"; //MessageBox.Show(platequery); SqlCommand commandtloc = new SqlCommand(tlocquery, cnn); SqlDataReader sdrtloc = commandtloc.ExecuteReader(); while (sdrtloc.Read()) { string Tlong = sdrtloc["LONG"].ToString(); txtbxTlong.Text = Tlong.ToString(); string Tlat = sdrtloc["LAT"].ToString(); txtbxTlat.Text = Tlat.ToString(); string Lac = sdrtloc["LAC"].ToString(); txtbxLac.Text = Lac.ToString(); string Cid = sdrtloc["CID"].ToString(); txtbxCid.Text = Cid.ToString(); string Street = sdrtloc["STREET"].ToString(); string Municipal = sdrtloc["MUNICIPAL"].ToString(); txtbxTloc.Text = Street.ToString() + "," + Municipal.ToString(); } string speed = input.Substring(46, 5); txtbxSpeedFast.Text = speed; string platequery = "SELECT plateno FROM ACCOUNT WHERE senderno =" + "'" + account + "'"; //MessageBox.Show(platequery); SqlCommand commandplate = new SqlCommand(platequery, cnn); SqlDataReader sdrplate = commandplate.ExecuteReader(); while (sdrplate.Read()) { string plate = sdrplate["plateno"].ToString(); txtbxActivePlate.Text = plate.ToString(); } string locquery = "SELECT NAME,MUNICIPALI FROM ROAD WHERE lat LIKE " + "'%" + tlatcut.ToString() + "%' AND long LIKE '%" + tlongcut.ToString() + "%'"; //MessageBox.Show(platequery); SqlCommand commandloc = new SqlCommand(locquery, cnn); SqlDataReader sdrloc = commandloc.ExecuteReader(); while (sdrloc.Read()) { string name = sdrloc["NAME"].ToString(); string municipali = sdrloc["MUNICIPALI"].ToString(); txtbxLocation.Text = name.ToString() + "," + municipali.ToString(); } string engine = input.Substring(21, 1); if (engine == "6" || engine == "2") { txtbxEngineFast.Text = "ON"; } else if (engine == "4" || engine == "0") { txtbxEngineFast.Text = "OFF"; } if (speed == "000.0") { txtbxRemarks.Text = "Stopped"; } else { txtbxRemarks.Text = "Running"; } string alert1 = input.Substring(20, 1); txtbxEvents.Text = alert1; string odometer = input.Substring(91, 6); //Convert hex to decimal and compute Odometer START int kmrun = Convert.ToInt32(odometer, 16); double kmfin = kmrun / 1000 * 1.852; //Convert hex to decimal and compute Odometer END txtbxOdometerActive.Text = kmfin.ToString(); if (alert1 == "2") { txtbxAlertActive.Text = "CIRCUIT CUT-OFF"; } else if (alert1 == "4") { txtbxAlertActive.Text = "Over the speed limit"; } else if (alert1 == "6") { txtbxAlertActive.Text = "Over the speed limit and Circuit Cut-off"; } else if (alert1 == "0") { if (engine == "1") { txtbxAlertActive.Text = "Panic Button"; } else if (engine == "3" || engine == "7") { txtbxAlertActive.Text = "Panic Button"; txtbxEngineFast.Text = "ON"; } else if (engine == "5") { txtbxAlertActive.Text = "Panic Button"; txtbxEngineFast.Text = "OFF"; } else if (engine == "8" || engine == "C") { txtbxAlertActive.Text = "Seatbelt ON"; } else if (engine == "9" || engine == "D") { txtbxAlertActive.Text = "Panic Button and Seatbelt ON"; } else if (engine == "A" || engine == "E") { txtbxAlertActive.Text = "Seatbelt ON"; txtbxEngineFast.Text = "ON"; } else if (engine == "B" || engine == "F") { txtbxAlertActive.Text = "Seatbelt ON and Panic Button"; txtbxEngineFast.Text = "ON"; } } string status = input.Substring(24, 1); if (status == "A") { txtbxDevStatus.Text = "OK"; } else { txtbxDevStatus.Text = "Message Not Valid"; } string determinator = input.Substring(16, 3); if (determinator == "RA1") { txtbxDevType.Text = "R10-V2"; } txtbxLastValidDateTime.Text = finnow.ToString(); } else { MessageBox.Show("NOT 105"); } } }
you can use while(true) loop or for( ; ; ) for never ending loop
The given value of type DateTime from the data source cannot be converted to type decimal of the spe
I am trying to migrate data from Oracle to SQL. I already created Table name and field Name. Type and Size are same on both: e.g. at Oracle Varchar2(11), On SQL VARCHAR(11) at Oracle Date, On SQL datetime2(0). In my application I'm using SqlBulkCopy function. But I face the above error on Date filed. private void Processing(string sPath) { string AppPath = Application.StartupPath.ToString(); IniFile myIni = new IniFile(sPath); string allTable = myIni.IniReadValue("TABLENAME", "TABLE"); string[] alltables = allTable.Split(','); foreach (var tableName in alltables) { string whereSqlQuery = string.Empty; string sOrderBySqlQuery = string.Empty; string sSQLSelect = string.Empty; string sRCount = string.Empty; whereSqlQuery = myIni.IniReadValue(tableName, "WHERE"); TableName = tableName; sOrderBySqlQuery = myIni.IniReadValue(tableName, "ORDERBY"); sSQLSelect = myIni.IniReadValue(tableName, "SQLSELECT"); //sRCount = myIni.IniReadValue(tableName, "RCOUNT"); if (radAuto.Checked == true) ConnectAndQuery(tableName, whereSqlQuery, sOrderBySqlQuery, sSQLSelect, sRCount); else ConnectAndQuery(tableName, whereSqlQuery, sOrderBySqlQuery); } } private void ConnectAndQuery(string strTableName, string strWhere, string strOrderBy, string sSqlSelect, string sRcount) { Cursor.Current = Cursors.WaitCursor; string connectionString = GetConnectionString(); // Using OracleConnection connection = new OracleConnection(); SqlConnection oConn = default(SqlConnection); DataTable dtSQL = null; DateTime dtLog = DateTime.Now; try { //Opening Oracle DB Connection connection.ConnectionString = connectionString; connection.Open(); ShownLog(string.Format("Table = {0} , -- STARTING --", strTableName)); ShownLog("Oracle Connection Opened, OK"); OracleCommand command = connection.CreateCommand(); if (!string.IsNullOrEmpty(strWhere)) SqlQuery = "SELECT * FROM " + strTableName + " WHERE "+ strWhere; else SqlQuery = string.Format("SELECT * FROM {0} ", strTableName); if (!string.IsNullOrEmpty(strOrderBy)) SqlQuery += " ORDER BY " + strOrderBy; command.CommandText = SqlQuery; System.DateTime startTime = System.DateTime.Now; ShownLog("Starting Date Time : " + startTime); DataTable dtTotalInsertCount = new DataTable(strTableName); if (!string.IsNullOrEmpty(sRcount)) { //OracleDataAdapter adpCount = new OracleDataAdapter(sRcount, connectionString); ////adpCount.FillSchema(dtTotalInsertCount, SchemaType.Source); //adpCount.Fill(dtTotalInsertCount); } OracleDataAdapter adapter = new OracleDataAdapter(SqlQuery, connectionString); DataTable dt = new DataTable(strTableName); adapter.FillSchema(dt, SchemaType.Source); bool valid = true; bool bCheck = true; int count = 1000; int start = 0; string sLogQuery = SqlQuery.Replace("'", ""); dtLog = DateTime.Now; //Insert to SQL Log Table Insert(strTableName, dtLog, sLogQuery); int iLogCount = 0; int iActualCount = 0; do { int iEnd = 0; adapter.Fill(start, count, dt); valid = dt.Rows.Count > 0 ? true : false; if (valid) { iLogCount += dt.Rows.Count; iEnd = start + dt.Rows.Count; ShownLog("No of data Rows retrieved from Oracle, Count = " + dt.Rows.Count); //Create the SQL Server Table oConn = new SqlConnection(txtDestinationConnString.Text); oConn.Open(); ShownLog("SQL Connection Opened, OK"); if (string.IsNullOrEmpty(sSqlSelect)) bCheck = false; if (bCheck) { ShownLog("Data Comparision Start : " + System.DateTime.Now); //If SQL Select Statement has if (!string.IsNullOrEmpty(sSqlSelect)) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = sSqlSelect; cmd.Connection = oConn; cmd.CommandType = CommandType.Text; SqlDataReader reader = cmd.ExecuteReader(); dtSQL = new DataTable(); dtSQL.Load(reader); cmd = null; reader = null; //Return record has more than 0 ShownLog("Start Duplicate Checking : " + System.DateTime.Now); if (dtSQL.Rows.Count > 0) { foreach (DataRow dr in dtSQL.Rows) { DataRow[] result; if (strTableName == "ITK_STAFF" || strTableName == "ITK_FLIGHT") { result = dt.Select("URNO='" + dr[0].ToString() + "'"); } else result = dt.Select("URNO=" + dr[0]); if (result.Length > 0) { foreach (var drRemove in result) { dt.Rows.Remove(drRemove); } } } } ShownLog("End Duplicate Checking : " + System.DateTime.Now); } ShownLog("Actual No.s of records to insert : " + dt.Rows.Count); if (dt.Rows.Count == count) bCheck = false; ShownLog("Data Comparision End : " + System.DateTime.Now); } if (valid) { ShownLog(System.DateTime.Now + " Copying " + strTableName + " From : " + start.ToString() + " To : " + iEnd); } start += count; if (dt.Rows.Count > 0) { iActualCount += dt.Rows.Count; CopyData(dt, oConn); dt.Rows.Clear(); } oConn.Close(); oConn = null; } } while (valid); System.DateTime endTime = System.DateTime.Now; ShownLog("Ending DateTime: " + endTime); //msgOut("No of rows copied from oracle to SQL Server , Count = " + dt.Rows.Count); TimeSpan diffTime = endTime.Subtract(startTime); ShownLog(String.Format("Time Difference is Days : {0}, Hours : {1}, Minites : {2}, seconds : {3} ,Milliseconds : {4}", diffTime.Days, diffTime.Hours, diffTime.Minutes, diffTime.Seconds, diffTime.Milliseconds)); ShownLog(string.Format("Table = {0} , -- FINISHED --", strTableName)); ShownLog(string.Empty); Update(iLogCount, iActualCount, strTableName, dtLog, sLogQuery, " ", true); Cursor.Current = Cursors.Default; } catch (Exception ex) { Cursor.Current = Cursors.Default; ShownLog(ex.ToString()); ShownLog(string.Empty); Update(0, 0, strTableName, dtLog, "", ex.ToString(), false); ((IDisposable)connection).Dispose(); if (oConn != null) oConn.Close(); RadioButtonControl(); } finally { Cursor.Current = Cursors.Default; ((IDisposable)connection).Dispose(); if (oConn != null) oConn.Close(); RadioButtonControl(); } } private void CopyData(DataTable sourceTable, SqlConnection destConnection) { // Using SqlBulkCopy s = new SqlBulkCopy(destConnection); try { s.DestinationTableName = TableName; s.NotifyAfter = 10000; s.SqlRowsCopied += new SqlRowsCopiedEventHandler(s_SqlRowsCopied); s.WriteToServer(sourceTable); s.Close(); } finally { ((IDisposable)s).Dispose(); } }