DataAccess

This is a  Simple  article which uses the DataGrid and Explains the DataAccess Mechanisms.
 
Simple DataAccessing startegies like
  1. Data Insertion.
  2. Data Searching and Data Modification .
This  uses the Reference Concepts in modifying the Data which matches the Search Criteria.
 
Application Logic and Presentation logic has been given separately.
 
3 DataGrids are given in  Presentation Logic.
  • DataGrid1 BindGrids to the Default Contents of the table Data.
  • DataGrid2 Inserted Data.
  • Data Grid3 shows the searched and the modified Data.
Each Functionality is written under the Event of the Corresponding click Events  of the Button.

Application Logic
  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Data.OleDb;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.HtmlControls;  
  12. namespace DataGridUpdate {  
  13.     /// <summary>  
  14.     /// Summary description for WebForm1.  
  15.     /// </summary>  
  16.     public class WebForm1: System.Web.UI.Page {  
  17.         protected System.Web.UI.WebControls.Button Button1;  
  18.         protected System.Web.UI.WebControls.Button Button2;  
  19.         protected System.Web.UI.WebControls.Button Button3;  
  20.         protected System.Web.UI.WebControls.DataGrid DataGrid1;  
  21.         protected System.Web.UI.WebControls.DataGrid DataGrid2;  
  22.         protected System.Web.UI.WebControls.DataGrid DataGrid3;  
  23.         protected System.Web.UI.WebControls.Label Label1;  
  24.         protected System.Web.UI.WebControls.Label Label2;  
  25.         protected System.Web.UI.WebControls.Label Label3;  
  26.         protected System.Web.UI.WebControls.Label Label4;  
  27.         public OleDbDataAdapter odap;  
  28.         public OleDbConnection Con;  
  29.         protected System.Web.UI.WebControls.TextBox TextBox1;  
  30.         protected System.Web.UI.WebControls.Label Label5;  
  31.         public DataSet data;  
  32.         private void Page_Load(object sender, System.EventArgs e) {  
  33.             // Put user code to initialize the page here  
  34.             BindGrid();  
  35.         }  
  36.         #region Web Form Designer generated code  
  37.         override protected void OnInit(EventArgs e) {  
  38.             //  
  39.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.  
  40.             //  
  41.             InitializeComponent();  
  42.             base.OnInit(e);  
  43.         }  
  44.         /// <summary>  
  45.         /// Required method for Designer support - do not modify  
  46.         /// the contents of this method with the code editor.  
  47.         /// </summary>  
  48.         private void InitializeComponent() {  
  49.             this.Button1.Click += new System.EventHandler(this.Button1_Click);  
  50.             this.Button2.Click += new System.EventHandler(this.Button2_Click);  
  51.             this.Load += new System.EventHandler(this.Page_Load);  
  52.         }  
  53.         #endregion  
  54.         public void BindGrid() {  
  55.             Con = new OleDbConnection(@ "Provider=Microsoft.Jet.OleDb.4.0;Data Source =c:\robert\Emp.mdb");  
  56.             odap = new OleDbDataAdapter("select * from emp", Con);  
  57.             data = new DataSet();  
  58.             odap.Fill(data, "emp");  
  59.             DataGrid1.DataSource = data.Tables[0];  
  60.             DataGrid1.DataBind();  
  61.         }  
  62.         private void Button1_Click(object sender, System.EventArgs e) {  
  63.             DataTable table = data.Tables[0];  
  64.             DataRow row = null;  
  65.             row = table.NewRow();  
  66.             row[0] = 65;  
  67.             row[1] = "Kumar";  
  68.             row[2] = 125655;  
  69.             table.Rows.Add(row);  
  70.             DataGrid2.DataSource = table;  
  71.             DataGrid2.DataBind();  
  72.         }  
  73.         private void Button2_Click(object sender, System.EventArgs e) {  
  74.             DataTable table = data.Tables[0];  
  75.             string Qstr = "empid ='" + TextBox1.Text.Trim() + "'";  
  76.             DataRow[] row = table.Select(Qstr);  
  77.             foreach(DataRow r in row) {  
  78.                 // Iam Modifying only the retrieved Content this is automatically reflected in the DataSource table;  
  79.                 r[1] = "Ajith";  
  80.                 r[2] = 787879;  
  81.             }  
  82.             DataGrid3.DataSource = table;  
  83.             DataGrid3.DataBind();  
  84.         }  
  85.     }  
  86. }  
Presentation Logic 
  1. <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridUpdate.WebForm1" %>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >  
  3. <HTML>  
  4.     <HEAD>  
  5.         <title>WebForm1</title>  
  6.         <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">  
  7.             <meta name="CODE_LANGUAGE" Content="C#">  
  8.                 <meta name="vs_defaultClientScript" content="JavaScript">  
  9.                     <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">  
  10.                     </HEAD>  
  11.                     <body MS_POSITIONING="GridLayout">  
  12.                         <form id="Form1" method="post" runat="server">  
  13.                             <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 600px" runat="server"  
  14.   
  15.     Text="AddNewRow" Width="128px"></asp:Button>  
  16.                             <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 600px"   
  17.   
  18.      runat="server"  
  19.   
  20.     Text="Edit Row" Width="136px"></asp:Button>  
  21.                             <asp:Button id="Button3" style="Z-INDEX: 103; LEFT: 504px; POSITION: absolute; TOP: 600px"  
  22.   
  23.      runat="server"  
  24.   
  25.     Text="Refresh" Width="144px"></asp:Button>  
  26.                             <asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 96px; POSITION: absolute; TOP: 192px"  
  27.   
  28.     runat="server"></asp:DataGrid>  
  29.                             <asp:DataGrid id="DataGrid2" style="Z-INDEX: 105; LEFT: 408px; POSITION: absolute; TOP: 192px"  
  30.   
  31.     runat="server"></asp:DataGrid>  
  32.                             <asp:DataGrid id="DataGrid3" style="Z-INDEX: 106; LEFT: 704px; POSITION: absolute; TOP: 200px"  
  33.   
  34.     runat="server"></asp:DataGrid>  
  35.                             <asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 368px; POSITION: absolute; TOP: 16px" runat="server"  
  36.   
  37.     Width="408px" Height="32px" Font-Size="Medium">Implementing DataGrid Action From Application  
  38.   
  39.      Logic</asp:Label>  
  40.                             <asp:Label id="Label2" style="Z-INDEX: 108; LEFT: 112px; POSITION: absolute; TOP: 136px" runat="server"  
  41.   
  42.     Width="147px">Existing Data</asp:Label>  
  43.                             <asp:Label id="Label3" style="Z-INDEX: 109; LEFT: 416px; POSITION: absolute; TOP: 144px" runat="server"  
  44.   
  45.     Width="136px">Added Row Data</asp:Label>  
  46.                             <asp:Label id="Label4" style="Z-INDEX: 110; LEFT: 712px; POSITION: absolute; TOP: 144px" runat="server"  
  47.   
  48.     Width="136px">Edited Row Data</asp:Label>  
  49.                             <asp:TextBox id="TextBox1" style="Z-INDEX: 111; LEFT: 656px; POSITION: absolute; TOP: 512px"  
  50.   
  51.     runat="server" Width="104px" Height="24px"></asp:TextBox>  
  52.                             <asp:Label id="Label5" style="Z-INDEX: 112; LEFT: 272px; POSITION: absolute; TOP: 528px" runat="server"  
  53.   
  54.     Width="225px" Height="16px">Enter Employee ID For Search</asp:Label>  
  55.                         </form>  
  56.                     </body>  
  57.                 </HTML>  
Application Logic
  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Data.OleDb;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.HtmlControls;  
  12. namespace DataGridUpdate {  
  13.     /// <summary>  
  14.     /// Summary description for WebForm1.  
  15.     /// </summary>  
  16.     public class WebForm1: System.Web.UI.Page {  
  17.         protected System.Web.UI.WebControls.Button Button1;  
  18.         protected System.Web.UI.WebControls.Button Button2;  
  19.         protected System.Web.UI.WebControls.Button Button3;  
  20.         protected System.Web.UI.WebControls.DataGrid DataGrid1;  
  21.         protected System.Web.UI.WebControls.DataGrid DataGrid2;  
  22.         protected System.Web.UI.WebControls.DataGrid DataGrid3;  
  23.         protected System.Web.UI.WebControls.Label Label1;  
  24.         protected System.Web.UI.WebControls.Label Label2;  
  25.         protected System.Web.UI.WebControls.Label Label3;  
  26.         protected System.Web.UI.WebControls.Label Label4;  
  27.         public OleDbDataAdapter odap;  
  28.         public OleDbConnection Con;  
  29.         protected System.Web.UI.WebControls.TextBox TextBox1;  
  30.         protected System.Web.UI.WebControls.Label Label5;  
  31.         public DataSet data;  
  32.         private void Page_Load(object sender, System.EventArgs e) {  
  33.             // Put user code to initialize the page here  
  34.             BindGrid();  
  35.         }  
  36.         #region Web Form Designer generated code  
  37.         override protected void OnInit(EventArgs e) {  
  38.             //  
  39.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.  
  40.             //  
  41.             InitializeComponent();  
  42.             base.OnInit(e);  
  43.         }  
  44.         /// <summary>  
  45.         /// Required method for Designer support - do not modify  
  46.         /// the contents of this method with the code editor.  
  47.         /// </summary>  
  48.         private void InitializeComponent() {  
  49.             this.Button1.Click += new System.EventHandler(this.Button1_Click);  
  50.             this.Button2.Click += new System.EventHandler(this.Button2_Click);  
  51.             this.Load += new System.EventHandler(this.Page_Load);  
  52.         }  
  53.         #endregion  
  54.         public void BindGrid() {  
  55.             Con = new OleDbConnection(@ "Provider=Microsoft.Jet.OleDb.4.0;Data Source =c:\robert\Emp.mdb");  
  56.             odap = new OleDbDataAdapter("select * from emp", Con);  
  57.             data = new DataSet();  
  58.             odap.Fill(data, "emp");  
  59.             DataGrid1.DataSource = data.Tables[0];  
  60.             DataGrid1.DataBind();  
  61.         }  
  62.         private void Button1_Click(object sender, System.EventArgs e) {  
  63.             DataTable table = data.Tables[0];  
  64.             DataRow row = null;  
  65.             row = table.NewRow();  
  66.             row[0] = 65;  
  67.             row[1] = "Kumar";  
  68.             row[2] = 125655;  
  69.             table.Rows.Add(row);  
  70.             DataGrid2.DataSource = table;  
  71.             DataGrid2.DataBind();  
  72.         }  
  73.         private void Button2_Click(object sender, System.EventArgs e) {  
  74.             DataTable table = data.Tables[0];  
  75.             string Qstr = "empid ='" + TextBox1.Text.Trim() + "'";  
  76.             DataRow[] row = table.Select(Qstr);  
  77.             foreach(DataRow r in row) {  
  78.                 // Iam Modifying only the retrieved Content this is automatically reflected in the DataSource table;  
  79.                 r[1] = "Ajith";  
  80.                 r[2] = 787879;  
  81.             }  
  82.             DataGrid3.DataSource = table;  
  83.             DataGrid3.DataBind();  
  84.         }  
  85.     }  
  86. }  
Presentation Logic
  1. <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridUpdate.WebForm1" %>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >  
  3. <HTML>  
  4.     <HEAD>  
  5.         <title>WebForm1</title>  
  6.         <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">  
  7.             <meta name="CODE_LANGUAGE" Content="C#">  
  8.                 <meta name="vs_defaultClientScript" content="JavaScript">  
  9.                     <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">  
  10.                     </HEAD>  
  11.                     <body MS_POSITIONING="GridLayout">  
  12.                         <form id="Form1" method="post" runat="server">  
  13.                             <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 600px" runat="server"  
  14.   
  15.     Text="AddNewRow" Width="128px"></asp:Button>  
  16.                             <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 600px"  
  17.   
  18.      runat="server" Text="Edit Row" Width="136px"></asp:Button>  
  19.                             <asp:Button id="Button3" style="Z-INDEX: 103; LEFT: 504px; POSITION: absolute; TOP: 600px"   
  20.   
  21.      runat="server"  Text="Refresh" Width="144px"></asp:Button>  
  22.                             <asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 96px; POSITION: absolute; TOP: 192px"  
  23.   
  24.     runat="server"></asp:DataGrid>  
  25.                             <asp:DataGrid id="DataGrid2" style="Z-INDEX: 105; LEFT: 408px; POSITION: absolute; TOP: 192px"  
  26.   
  27.     runat="server"></asp:DataGrid>  
  28.                             <asp:DataGrid id="DataGrid3" style="Z-INDEX: 106; LEFT: 704px; POSITION: absolute; TOP: 200px"  
  29.   
  30.     runat="server"></asp:DataGrid>  
  31.                             <asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 368px; POSITION: absolute; TOP: 16px" runat="server"  
  32.   
  33.     Width="408px" Height="32px" Font-Size="Medium">Implementing DataGrid Action From Application  
  34.   
  35.      Logic</asp:Label>  
  36.                             <asp:Label id="Label2" style="Z-INDEX: 108; LEFT: 112px; POSITION: absolute; TOP: 136px" runat="server"  
  37.   
  38.     Width="147px">Existing Data</asp:Label>  
  39.                             <asp:Label id="Label3" style="Z-INDEX: 109; LEFT: 416px; POSITION: absolute; TOP: 144px" runat="server"  
  40.   
  41.     Width="136px">Added Row Data</asp:Label>  
  42.                             <asp:Label id="Label4" style="Z-INDEX: 110; LEFT: 712px; POSITION: absolute; TOP: 144px" runat="server"  
  43.   
  44.     Width="136px">Edited Row Data</asp:Label>  
  45.                             <asp:TextBox id="TextBox1" style="Z-INDEX: 111; LEFT: 656px; POSITION: absolute; TOP: 512px"  
  46.   
  47.     runat="server" Width="104px" Height="24px"></asp:TextBox>  
  48.                             <asp:Label id="Label5" style="Z-INDEX: 112; LEFT: 272px; POSITION: absolute; TOP: 528px" runat="server"  
  49.   
  50.     Width="225px" Height="16px">Enter Employee ID For Search</asp:Label>  
  51.                         </form>  
  52.                     </body>  
  53.                 </HTML>   


Similar Articles