Monday, 2 April 2018

006 ADO.Net Datatable

DataTable is essential part ADO.Net . DataTables is created in memory and hold data in tabular format. DataTable comes under the namespace System.Data. DataSet content one or multiple DataTable . DataSet contain table and data in XML format. Here is some commonly used attribute of a DataTable.

  • Constructor 
  1. DataTable : Create an instance of the DataTable with new operator. 
  2. DataTable (string tableName) : Create a new instance of a DataTable with specific name.
  • Property 
  1. Columns : Get the collection of columns in a DataTable. 
  2. DataSet : Get the parent DataSet of a table. 
  3. DefaultView: Give the view of a specific query /filter condition output. 
  4. HasError : Get indication that a DataTable has error or not. 
  5. Rows : Get the collection of rows. 
  6. TableName : Get hot Set the name of the table. 
  • Method 
  1. AcceptChanges : Commint all changes since the DataTable has last loaded. 
  2. Clear : Clear all rows and column data. 
  3. Copy : Copy a DataTable  to another DataTable . 
  4. Dispose : Release all resource hold by the DataTable .
  5. Load : Fill DataTable with value from Data sources.
  6. Merge : Merge a DataTable with another table.
  7. NewRow: Create a new row with same structure of the table row, that can hold data. 
  8. ReadXML : Read XML schema with data and insert into the data table. 
  9. RejectChanges : Rollback all changes that have been made since the DataTable last loaded. 
  10. Select : Return Collection of data row when filter criteria apply.
  11. Write XML : Write the current DataTable to XML File.  


 
Datatable play an important role in ado.Net framework. DataTable can be created programmatically and data and data column can be added deleted programmatically . DataSet content one or multiple table. DataTable allow us select filter data, return as data view. DataTable is faster in performance and it is in memory representation of a data.

 
1) Dynamically add row and column to Datatable 


using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.OleDb; 


namespace ConsoleApplication1
{
            class Program
            {
                  static void Main(string[] args)
                  {
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Name");
                    dt.Columns.Add("RollNumber");

                    DataRow nr1 = dt.NewRow();
                    nr1["Name"] = "John1";
                    nr1["RollNumber"] = 500;
                    dt.Rows.Add(nr1);

                   DataRow nr2 = dt.NewRow();
                  nr2["Name"] = "John2";
                  nr2["RollNumber"] = 600;
                  dt.Rows.Add(nr2);

                 DataRow nr3 = dt.NewRow();
                 nr3["Name"] = "John3";
                 nr3["RollNumber"] = 700;
         dt.Rows.Add(nr3);
                ds.Merge(dt);


                DataTable dt2 = new DataTable();
                dt2.Columns.Add("Address");
         dt2.Columns.Add("ZIP");


                 DataRow nr4 = dt2.NewRow();
                 nr4["Address"] = "3,Kolkata";
         nr4["ZIP"] = 500;

                dt2.Rows.Add(nr4);
                DataRow nr5 = dt2.NewRow();
               nr5["Address"] = "4,Mumbai";
               nr5["ZIP"] = 600;
               dt2.Rows.Add(nr5);

               DataRow nr6 = dt2.NewRow();
               nr6["Address"] = "5,Bombay";
               nr6["ZIP"] = 700;
              dt2.Rows.Add(nr6);
              ds.Merge(dt2);


for (int i = 0; i < dt.Rows.Count; i++)
       {
               for (int l = 0; l < dt.Columns.Count; l++)
                  {
                  Console.Write(dt.Rows[i][l].ToString());

                                 if (l == 0)
                                                 Console.Write("|");
                                 if (l == 1)
                                                 Console.WriteLine("");
                }
}
          Console.ReadKey();
             }
     }
}


Output :
John1|500
John2|600
John3|700

2) Delete row and column from the Datatable 
 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.OleDb;
namespace ConsoleApplication1
{
            class Program
           {
                       static void Main(string[] args)
                        {
                                   DataTable dt = new DataTable();
                                   dt.Columns.Add("Name");
                                   dt.Columns.Add("RollNumber");

                                    DataRow nr1 = dt.NewRow();
                                    nr1["Name"] = "John1";
                                   nr1["RollNumber"] = 500;
                                   dt.Rows.Add(nr1);


                   DataRow nr2 = dt.NewRow();
                   nr2["Name"] = "John2";
                   nr2["RollNumber"] = 600;
                   dt.Rows.Add(nr2);

                   DataRow nr3 = dt.NewRow();
                   nr3["Name"] = "John3";
                   nr3["RollNumber"] = 700;
                  dt.Rows.Add(nr3);


Console.WriteLine("-----------------Original Table Data-----------------");
for (int i = 0; i < dt.Rows.Count; i++)
{
                   for (int l = 0; l < dt.Columns.Count; l++)
                   {
                                  Console.Write(dt.Rows[i][l].ToString());
                                 if (l==0)
                                              Console.Write("|");
                                 if (l==1)
                                                Console.WriteLine("");
                   }
}
//Delete a row
dt.Rows[0].Delete();

Console.WriteLine("-----------------After Delete Row-----------------");

for (int i = 0; i < dt.Rows.Count; i++)
{
                        for (int l = 0; l < dt.Columns.Count; l++)
                       {
                           Console.Write(dt.Rows[i][l].ToString());

                           if (l == 0)
                           Console.Write("|");

                          if (l == 1)
                             Console.WriteLine("");
            }

}
dt.Columns.Remove("RollNumber");
Console.WriteLine("-----------------After Delete Column RollNumber-----------------");






for (int i = 0; i < dt.Rows.Count; i++)
{
              for (int l = 0; l < dt.Columns.Count; l++)
             {
                           Console.Write(dt.Rows[i][l].ToString());

                            if (l == 0)
                             Console.Write("|");
                             Console.WriteLine("");
               }
         }
      Console.ReadKey();
       }
   }
}


OutPut :
-----------------Original Table Data-----------------
John1|500
John2|600
John3|700
-----------------After Delete Row-----------------
John2|600
John3|700
-----------------After Delete Column RollNumber-----------------
John2|
John3|



3) Filter criteria on the Datatable 
 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.OleDb;

namespace ConsoleApplication1
{
          class Program
      {
            static void Main(string[] args)
           {
             DataTable dt = new DataTable();
       dt.Columns.Add("Name");
             dt.Columns.Add("RollNumber");

       DataRow nr1 = dt.NewRow();
       nr1["Name"] = "John1";
            nr1["RollNumber"] = 500;
           dt.Rows.Add(nr1);

           DataRow nr2 = dt.NewRow();
           nr2["Name"] = "John2";
          nr2["RollNumber"] = 600;
     dt.Rows.Add(nr2);


         DataRow nr3 = dt.NewRow();
         nr3["Name"] = "John3";
         nr3["RollNumber"] = 700;
         dt.Rows.Add(nr3);


    Console.WriteLine("-----------------Original Table Data-----------------");

for (int i = 0; i < dt.Rows.Count; i++)
      {
           for (int l = 0; l < dt.Columns.Count; l++)
                {
                    Console.Write(dt.Rows[i][l].ToString());
                    if (l == 0)
                                               Console.Write("|");

                    if (l == 1)
                                             Console.WriteLine("");
               }
}
Console.WriteLine("-------Example of equal filter criteria :RollNumber=600----");
 
DataRow[] drs= dt.Select("RollNumber=600");
foreach (DataRow d in drs)
{
       Console.Write(d[0]+"|"+d[1]);
}

Console.WriteLine("");
Console.WriteLine("-------Example of Greater than filter criteria :RollNumber>600----");

 
DataRow[] drs1 = dt.Select("RollNumber>600");
foreach (DataRow d in drs1)
{
       Console.Write(d[0] + "|" + d[1]);
}
Console.WriteLine("");
Console.WriteLine("-------Example of Range filter criteria :RollNumber>500 AND RollNumber);
     DataRow[] drs2 = dt.Select("RollNumber>500 AND RollNumber);
     foreach (DataRow d in drs2)
     {
              Console.Write(d[0] + "|" + d[1]);
     }
 
Console.WriteLine("");
 
Console.WriteLine("-------Example of Range filter criteria :Name like 'John3'----");
 
     DataRow[] drs3 = dt.Select("Name like '%John3%'");
     foreach (DataRow d in drs3)
     {
       Console.Write(d[0] + "|" + d[1]);
     }
Console.ReadKey();
   }
  }
}


Output  :
-----------------Original Table Data-----------------
John1|500
John2|600
John3|700
-------Example of equal filter criteria :RollNumber=600----
John2|600
-------Example of Greater than  filter criteria :RollNumber>600----
John3|700
-------Example of Range  filter criteria :RollNumber>500 AND RollNumberJohn2|600
-------Example of Range  filter criteria :Name like 'John3'----
John3|700




Thursday, 29 March 2018

0231 CACHING Web Form





Catching is a technique to store frequent use data and information into the memory temporarily. As the information and data is storeed in the memory, if this is requested again, instead of requesting server, data and information is fetch from the memory . This makes an website  faster in performance . Some pages are dynamically generated , these are cost heavy CPU usage  and time consuming. If this is catched , CPU usage reduce and delivery faster to the client.

Catching still  possible
  • Object lifetimes not expired 
  • An application has not released memories 
  • Error or exception did not occurred  during catching. 

In this chapter we will go to several kind of catching technique
  • Catching web forms 
  • Catching part of forms  
  • Catching application data 
  • Monitor catching performance
Catching Webforms : A full webform can we catched  and store into the memory , when Requested  it is delivered as it was. The directive OutputCache  directive to catch full page. Duration is a parameter to set how  long (in second)  the pages should keep the memory .

Below is the example of the same.
Example 1


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false" %>
<%@ OutputCache Duration="60" VaryByParam="*" %>

<!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>
           </div>
             <asp:Button ID="Button1" runat="server" Text="Submit" />
         </form>
  </body>
</html>

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
            protected void Page_Load(object sender, EventArgs e)
             {
                  Response.Write(DateTime.Now.ToString("h:mm:ss tt"));
              }

}






                                            (Notice that Time is same after postback)


The above example catch the waveform for 20 second .Output cache has two parameter , duration and VaryByParam. Duration tell the time limit the page should be kept in the memory and VaryByParam allowed to multiple resource that could be kept in the memory.

Example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false" %>
<%@ OutputCache Duration="60" VaryByParam="DropDownList1" %>

<!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:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>India</asp:ListItem>
                        <asp:ListItem>USA</asp:ListItem>
                        <asp:ListItem>UK</asp:ListItem>
                        <asp:ListItem>Brazil</asp:ListItem>
                        <asp:ListItem>Australia</asp:ListItem>
                  </asp:DropDownList>
          </div>
          <asp:Button ID="Button1" runat="server" Text="Submit" />
   </form>
</body>
</html>

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
              protected void Page_Load(object sender, EventArgs e)
             {
               Response.Write(DateTime.Now.ToString("h:mm:ss tt"));
             }
}


HttpCachePolicy class to control output caching, below is some commonly used method of

HttpCachePolicy class

ByValueParam : Catch multiple response from singular form.
ByValueHeader : Catch  multiple response from a single form based on http header. 
SetVaryByCustom: Catch it multiple response depending on string.  
SetExpires : How long the catch you would be in memory.  
SetCacheability : The location what to save  information  client host server  proxy server ect.


HttpCacheability is an enumaration for where the data / information will be stored.

* Option Stored Location
Any HttpCacheability.Server Any ,Client ,Proxy & Host
Client HttpCacheability.Private Client
DownStream HttpCacheability.Public Client or Proxy
Server HttpCacheability.ServerAndNoCache Host Server
Server and Client HttpCacheability.ServerAndPrivate Host Server & Client



Here is the use of HttpCacheability enumeration.The following syntax tell that Response will be catched for 60 second and it would be catched to Client or ProxyServer


Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);














বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...