Wednesday, March 9, 2011

Caching and Manipulating a DataTable in memory (without storing in SQL)

Default.aspx.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;usingSystem.IO;using System.Data;using System.Data.SqlClient;public
{


{

{
th =
th.AddRowWithVerification(
th.AddRowWithVerification(
th.AddRowWithVerification(
Session[
}

{
th =
}
GridView1.DataSource = th.GetTable();
GridView1.DataBind();
partial class _Default : System.Web.UI.Page TableHandler th;protected void Page_Load(object sender, EventArgs e)if (Session["THTable"] == null)new TableHandler();"aa11", "bb", "ccb", DateTime.Now);"aa2", "bb", "cc", DateTime.Now);"aa22", "bbb", "ccb", DateTime.Now);"THTable"] = th.GetTable();if (Session["THTable"] != null && th == null)new TableHandler();// Start date
DateTime startDate = new DateTime(2005, 2, 1, 3, 4, 12, 56);// End date
DateTime endDate = new DateTime(2005, 12, 12, 4, 30, 45, 12);// Time span
TimeSpan diffDate = endDate.Subtract(startDate);// Spit it out Label1.Text =

Label1.Text += (diffDate.Days.ToString() +
Label1.Text += (diffDate.Hours.ToString() +
Label1.Text += (diffDate.Minutes.ToString() +
Label1.Text += (diffDate.Seconds.ToString() +
Label1.Text += (diffDate.Milliseconds.ToString() +
Label1.Text +=
Label1.Text += diffDate.TotalMilliseconds +
Label1.Text +=
Label1.Text += (
}

{
th.AddRowWithVerification(TextBox1.Text,TextBox2.Text,TextBox3.Text,
GridView1.DataBind();
}
}

"Start Date:" + startDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>" + "End Date:" + endDate.ToString("yyyy-MM-dd HH:mm:ss.fff") +"<br>" + "Time Difference: ";" Days,");" Hours,");" Minutes,");" Seconds,");" Milliseconds " + "<br>");"Total TimeDifference: " + diffDate.TotalMinutes + "Minutes = " + diffDate.TotalHours + "Hours = ";"Millisecs = " + diffDate.TotalDays + "Days" + "<br>";DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>";DateTime.Now.AddMinutes(-30) < DateTime.Now) ? (DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff")) : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");protected void Button1_Click(object sender, EventArgs e)DateTime.Now);//pagepublic class TableHandler{


{

{
x =
x.Columns.Add(
x.Columns.Add(
x.Columns.Add(
x.Columns.Add(

dr[
dr[
dr[
dr[
x.Rows.Add(dr);
System.Web.
}
DataTable x;public TableHandler()if (System.Web.HttpContext.Current.Session["THTable"] == null)new DataTable();new DataColumn("IPAddress", System.Type.GetType("System.String")));new DataColumn("PortNo", System.Type.GetType("System.String")));new DataColumn("DeviceIMEINo", System.Type.GetType("System.String")));new DataColumn("DateTime", System.Type.GetType("System.DateTime")));DataRow dr = x.NewRow();"IPAddress"] = "sfds";"PortNo"] = "sdfds";"DeviceIMEINo"] = "dsfds";"DateTime"] = DateTime.Now;HttpContext.Current.Session["THTable"] = x;elsex = (
}

{
(
DeleteOldRecords();
DataTable)System.Web.HttpContext.Current.Session["THTable"];public void AddRowWithVerification(string IPAddress, string PortNo, string DeviceIMEINo, DateTime dtinn)new Utility()).WriteLog("C:\\test\\test.txt" ,"AddRowWithVerification Called");DataRow[] dr = x.Select("DeviceIMEINo = '" + DeviceIMEINo + "'");if (dr.Count() > 0) //row already exists , update it{

drr[
drr[
drr[
}
DataRow drr = dr[0];"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DateTime"] = dtinn;else //add new row{

drr[
drr[
drr[
drr[
x.Rows.Add(drr);
}
System.Web.
}
DataRow drr = x.NewRow();"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DeviceIMEINo"] = DeviceIMEINo;"DateTime"] = dtinn;HttpContext.Current.Session["THTable"] = x;//AddRowWithVerification
{

dr[
dr[
dr[
x.Rows.Add(dr);
System.Web.
}
public void AddRow(string IPAddress, string PortNo, string DeviceIMEINo)DataRow dr = x.NewRow();"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DeviceIMEINo"] = DeviceIMEINo;HttpContext.Current.Session["THTable"] = x;//AddRow
{

}
public DataTable GetTable()return (DataTable)System.Web.HttpContext.Current.Session["THTable"];//GetTable
{


{

{
drr.Delete();
(
}
}
}
void DeleteOldRecords()DataRow[] dr = x.Select("DateTime < '" + DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss.fff") + "'");if (dr.Count() > 0)foreach (DataRow drr in dr)new Utility()).WriteLog("C:\\test\\test.txt", "Deleted Some Records");//DeleteOldRecords
{


DeviceIMEIMo = dr[0][

}
public string GetDeviceIMEINo(string IPAddress)string DeviceIMEIMo = "";DataRow[] dr = x.Select("IPAddress = " + IPAddress.Trim());"DeviceIMEINo"].ToString().Trim();return DeviceIMEIMo;//GetDeviceIMEINo}



//TableHandlerpublic class Utility{

{


sr.WriteLine(LogString);
sr.Flush();
sr.Close();
}
}
public void WriteLog(string FileName, string LogString)FileStream ff = new FileStream(FileName, FileMode.Append);StreamWriter sr = new StreamWriter(ff);
Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!
<
<
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">html xmlns="http://www.w3.org/1999/xhtml">head runat="server">
</
<
<title></title>head>body><form id="form1" runat="server"><div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</
</
</form>body>html>





using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using
System.IO;using System.Data;using System.Data.SqlClient;public
{


{

{
th =
th.AddRowWithVerification(
th.AddRowWithVerification(
th.AddRowWithVerification(
Session[
}

{
th =
}
GridView1.DataSource = th.GetTable();
GridView1.DataBind();

partial class _Default : System.Web.UI.Page TableHandler th;protected void Page_Load(object sender, EventArgs e)if (Session["THTable"] == null)new TableHandler();"aa11", "bb", "ccb", DateTime.Now);"aa2", "bb", "cc", DateTime.Now);"aa22", "bbb", "ccb", DateTime.Now);"THTable"] = th.GetTable();if (Session["THTable"] != null && th == null)new TableHandler();// Start date
DateTime startDate = new DateTime(2005, 2, 1, 3, 4, 12, 56);// End date
DateTime endDate = new DateTime(2005, 12, 12, 4, 30, 45, 12);// Time span
TimeSpan diffDate = endDate.Subtract(startDate);// Spit it out Label1.Text =

Label1.Text += (diffDate.Days.ToString() +
Label1.Text += (diffDate.Hours.ToString() +
Label1.Text += (diffDate.Minutes.ToString() +
Label1.Text += (diffDate.Seconds.ToString() +
Label1.Text += (diffDate.Milliseconds.ToString() +
Label1.Text +=
Label1.Text += diffDate.TotalMilliseconds +
Label1.Text +=
Label1.Text += (
}

{
th.AddRowWithVerification(TextBox1.Text,TextBox2.Text,TextBox3.Text,
GridView1.DataBind();
}
}


"Start Date:" + startDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>" + "End Date:" + endDate.ToString("yyyy-MM-dd HH:mm:ss.fff") +"<br>" + "Time Difference: ";" Days,");" Hours,");" Minutes,");" Seconds,");" Milliseconds " + "<br>");"Total TimeDifference: " + diffDate.TotalMinutes + "Minutes = " + diffDate.TotalHours + "Hours = ";"Millisecs = " + diffDate.TotalDays + "Days" + "<br>";DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>";DateTime.Now.AddMinutes(-30) < DateTime.Now) ? (DateTime.Now.AddMinutes(-30).ToString("yyyy-MM-dd HH:mm:ss.fff")) : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");protected void Button1_Click(object sender, EventArgs e)DateTime.Now);//pagepublic class TableHandler{


{

{
x =
x.Columns.Add(
x.Columns.Add(
x.Columns.Add(
x.Columns.Add(

dr[
dr[
dr[
dr[
x.Rows.Add(dr);
System.Web.
}
DataTable x;public TableHandler()if (System.Web.HttpContext.Current.Session["THTable"] == null)new DataTable();new DataColumn("IPAddress", System.Type.GetType("System.String")));new DataColumn("PortNo", System.Type.GetType("System.String")));new DataColumn("DeviceIMEINo", System.Type.GetType("System.String")));new DataColumn("DateTime", System.Type.GetType("System.DateTime")));DataRow dr = x.NewRow();"IPAddress"] = "sfds";"PortNo"] = "sdfds";"DeviceIMEINo"] = "dsfds";"DateTime"] = DateTime.Now;HttpContext.Current.Session["THTable"] = x;elsex = (
}

{
(
DeleteOldRecords();

DataTable)System.Web.HttpContext.Current.Session["THTable"];public void AddRowWithVerification(string IPAddress, string PortNo, string DeviceIMEINo, DateTime dtinn)new Utility()).WriteLog("C:\\test\\test.txt" ,"AddRowWithVerification Called");DataRow[] dr = x.Select("DeviceIMEINo = '" + DeviceIMEINo + "'");if (dr.Count() > 0) //row already exists , update it{

drr[
drr[
drr[
}
DataRow drr = dr[0];"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DateTime"] = dtinn;else //add new row{

drr[
drr[
drr[
drr[
x.Rows.Add(drr);
}
System.Web.
}
DataRow drr = x.NewRow();"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DeviceIMEINo"] = DeviceIMEINo;"DateTime"] = dtinn;HttpContext.Current.Session["THTable"] = x;//AddRowWithVerification
{

dr[
dr[
dr[
x.Rows.Add(dr);
System.Web.
}
public void AddRow(string IPAddress, string PortNo, string DeviceIMEINo)DataRow dr = x.NewRow();"IPAddress"] = IPAddress;"PortNo"] = PortNo;"DeviceIMEINo"] = DeviceIMEINo;HttpContext.Current.Session["THTable"] = x;//AddRow
{

}
public DataTable GetTable()return (DataTable)System.Web.HttpContext.Current.Session["THTable"];//GetTable
{


{

{
drr.Delete();
(
}
}
}
void DeleteOldRecords()DataRow[] dr = x.Select("DateTime < '" + DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss.fff") + "'");if (dr.Count() > 0)foreach (DataRow drr in dr)new Utility()).WriteLog("C:\\test\\test.txt", "Deleted Some Records");//DeleteOldRecords
{


DeviceIMEIMo = dr[0][

}
public string GetDeviceIMEINo(string IPAddress)string DeviceIMEIMo = "";DataRow[] dr = x.Select("IPAddress = " + IPAddress.Trim());"DeviceIMEINo"].ToString().Trim();return DeviceIMEIMo;//GetDeviceIMEINo}




//TableHandlerpublic class Utility{

{


sr.WriteLine(LogString);
sr.Flush();
sr.Close();
}
}
public void WriteLog(string FileName, string LogString)FileStream ff = new FileStream(FileName, FileMode.Append);StreamWriter sr = new StreamWriter(ff);


No comments:

Post a Comment