public bool UpdateStockprice(bool IsBatch) { string sSName = ""; string sUpdates = ""; string [] strOnlinevalue; OleDbCommand cmdStockName = new OleDbCommand(); OleDbCommand cmdStockRate = new OleDbCommand(); OleDbDataReader drStock = null; cmdStockRate.Connection = _oConnDM; cmdStockRate.CommandType = CommandType.Text; cmdStockName.Connection = _oConnDM; cmdStockName.CommandType = CommandType.StoredProcedure; cmdStockName.CommandText = "QryActiveStocks"; cmdStockName.Parameters.Clear(); cmdStockName.Parameters.Add("@Active", OleDbType.Boolean).Value = false; if (!IsBatch) { _pBarDoubleM.Visible = true; } try { if (_oConnDM.State != ConnectionState.Open) { _oConnDM.Open(); } drStock = cmdStockName.ExecuteReader(); if (drStock.HasRows) { if (!IsBatch) { _pBarDoubleM.Value = 0; } while (drStock.Read()) //Reading Stock Names { if (!IsBatch) { _pBarDoubleM.Maximum = drStock.GetInt32(10) + 1; } sUpdates = ""; sSName = drStock.GetString(4); //4 - YFCode if (CommonDoubleM.YahooStockLatestUpdates(sSName, ref sUpdates)) { //Add latest Rates try { string sqlUpdateRate = "INSERT into [TRates] (StockID, PRICE, Ondate) VALUES (@StockID, @price, #@Ondate#)"; strOnlinevalue = CommonDoubleM.FilterOnlineValues(sUpdates); if (!IsBatch) { _lblMsgDoubleM.Text = "Latest value: " + sSName + " [" + strOnlinevalue[1] + "]"; } System.Windows.Forms.Application.DoEvents(); if (Convert.ToDouble(strOnlinevalue[1]) > 0) { sqlUpdateRate = sqlUpdateRate.Replace("@StockID", drStock.GetInt32(0).ToString()); sqlUpdateRate = sqlUpdateRate.Replace("@price", strOnlinevalue[1]); //sqlUpdateRate = sqlUpdateRate.Replace("@Ondate", strOnlinevalue[2].Trim('"', '/') + " " + strOnlinevalue[3].Trim('"', '/')); sqlUpdateRate = sqlUpdateRate.Replace("@Ondate", DateTime.Now.ToString("dd/MMMM/yyyy HH:mm:ss")); cmdStockRate.CommandText = sqlUpdateRate; cmdStockRate.ExecuteNonQuery(); } } catch (Exception ex1) { if (!IsBatch) { _lblMsgDoubleM.Text = ex1.Message + " Unable to update latest Rate of :" + sSName; } CommonDoubleM.LogDM("Unable to update latest Rate of :" + sSName); } if (!IsBatch) { _pBarDoubleM.Value = _pBarDoubleM.Value + 1; } } } } return(true); } catch (Exception ex) { if (!IsBatch) { _lblMsgDoubleM.Text = ex.Message; } CommonDoubleM.LogDM(ex.Message); return(false); } finally { cmdStockName.Dispose(); cmdStockRate.Dispose(); drStock.Close(); drStock.Dispose(); if (!IsBatch) { _pBarDoubleM.Visible = false; } if (_oConnDM.State != ConnectionState.Closed) { _oConnDM.Close(); } } }
private void DowloadAllRates() { string sDownloaded = ""; string sCode = ""; string[] strOnlinevalue; EnableForm(false); Cursor = Cursors.WaitCursor; try { _theParent.pBarDM.Visible = true; _theParent.pBarDM.Maximum = dgViewQ.Rows.Count; foreach (DataGridViewRow dr in dgViewQ.Rows) { _theParent.pBarDM.Value = dr.Index; if (blnSelectedUpdate) //for selected updates only { if (!dr.Selected) { continue; } } if (dr.Cells[11].Value == null) { continue; } if (dr.Cells[11].Value.ToString().Trim() == "") { continue; } sCode = dr.Cells[11].Value.ToString(); if (_Source == "Yahoo") { _theParent.lblDMMsg.Text = "Downloading from Yahoo finance"; if (CommonDoubleM.YahooStockLatestUpdates(sCode, ref sDownloaded) == false) { continue; } Application.DoEvents(); strOnlinevalue = CommonDoubleM.FilterOnlineValues(sDownloaded); if (Convert.ToDouble(strOnlinevalue[1]) > 0) { _theParent.lblDMMsg.Text = "Latest value: " + dr.Cells[0].Value.ToString() + " [" + strOnlinevalue[1] + "]"; dr.Cells[2].Value = strOnlinevalue[1]; dr.Cells[8].Value = strOnlinevalue[2].Replace("\"", "") + " " + strOnlinevalue[3].Replace("\"", ""); strOnlinevalue = strOnlinevalue[4].Split(' '); dr.Cells[5].Value = strOnlinevalue[2].Replace("+", "").Replace("\"", "").Replace("%", ""); } } else if (_Source == "Rediff") { _theParent.lblDMMsg.Text = "Downloading from Rediff money"; //Reading from Rediff returns true once the reading successfully if (CommonDoubleM.RediffStockLatestUpdates(sCode, ref sDownloaded) == false) { continue; } Application.DoEvents(); sDownloaded = CommonDoubleM.StripHTML(sDownloaded); //strOnlinevalue = strOnlinevalue.Split(cfilter); strOnlinevalue = sDownloaded.Split('\n', '\r', ' '); for (int i = 0; i < strOnlinevalue.Length; i++) { if (strOnlinevalue[i].Trim().Contains(sCode)) { //Means next element must be the Rate if (Convert.ToDouble(strOnlinevalue[i + 1]) > 0) { _theParent.lblDMMsg.Text = "Latest value: " + dr.Cells[0].Value.ToString() + " [" + strOnlinevalue[1 + 1] + "]"; dr.Cells[2].Value = strOnlinevalue[i + 1]; i++; } } if (strOnlinevalue[i].Trim().Contains("%")) { //Change % found dr.Cells[5].Value = strOnlinevalue[i].Replace("+", "").Replace("\"", "").Replace("%", "");; } if (strOnlinevalue[i].Trim().Contains(":")) { //Time : found dr.Cells[8].Value = strOnlinevalue[i - 2].Trim() + " " + strOnlinevalue[i - 1].Trim() + " " + DateTime.Now.Year.ToString() + " " + strOnlinevalue[i].Trim(); break; } } } //Auto Scroll to Row updating if (!dgViewQ.Rows[dr.Index + 1].Displayed) { dgViewQ.FirstDisplayedScrollingRowIndex = dr.Index; } //unselect the corrent row dr.Selected = false; } } catch (Exception ex) { _theParent.lblDMMsg.Text = ex.Message; } finally { blnSelectedUpdate = false; //Only selected updated thru, change the mode EnableForm(true); _theParent.pBarDM.Visible = false; Cursor = Cursors.Default; } }