This is a Simple article which uses the DataGrid and Explains the DataAccess Mechanisms.
Simple DataAccessing startegies like
- Data Insertion.
- 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
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace DataGridUpdate
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.WebControls.DataGrid DataGrid3;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
public OleDbDataAdapter odap;
public OleDbConnection Con;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label5;
public DataSet data;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
BindGrid();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void BindGrid()
{
Con = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source =c:\robert\Emp.mdb");
odap = new OleDbDataAdapter("select * from emp", Con);
data = new DataSet();
odap.Fill(data, "emp");
DataGrid1.DataSource = data.Tables[0];
DataGrid1.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
DataTable table = data.Tables[0];
DataRow row = null;
row = table.NewRow();
row[0] = 65;
row[1] = "Kumar";
row[2] = 125655;
table.Rows.Add(row);
DataGrid2.DataSource = table;
DataGrid2.DataBind();
}
private void Button2_Click(object sender, System.EventArgs e)
{
DataTable table = data.Tables[0];
string Qstr = "empid ='" + TextBox1.Text.Trim() + "'";
DataRow[] row = table.Select(Qstr);
foreach (DataRow r in row)
{
// Iam Modifying only the retrieved Content this is automatically reflected in the DataSource table;
r[1] = "Ajith";
r[2] = 787879;
}
DataGrid3.DataSource = table;
DataGrid3.DataBind();
}
}
}
Presentation Logic
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridUpdate.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 600px" runat="server"
Text="AddNewRow" Width="128px"></asp:Button>
<asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 600px"
runat="server"
Text="Edit Row" Width="136px"></asp:Button>
<asp:Button id="Button3" style="Z-INDEX: 103; LEFT: 504px; POSITION: absolute; TOP: 600px"
runat="server"
Text="Refresh" Width="144px"></asp:Button>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 96px; POSITION: absolute; TOP: 192px"
runat="server"></asp:DataGrid>
<asp:DataGrid id="DataGrid2" style="Z-INDEX: 105; LEFT: 408px; POSITION: absolute; TOP: 192px"
runat="server"></asp:DataGrid>
<asp:DataGrid id="DataGrid3" style="Z-INDEX: 106; LEFT: 704px; POSITION: absolute; TOP: 200px"
runat="server"></asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 368px; POSITION: absolute; TOP: 16px" runat="server"
Width="408px" Height="32px" Font-Size="Medium">Implementing DataGrid Action From Application
Logic</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 108; LEFT: 112px; POSITION: absolute; TOP: 136px" runat="server"
Width="147px">Existing Data</asp:Label>
<asp:Label id="Label3" style="Z-INDEX: 109; LEFT: 416px; POSITION: absolute; TOP: 144px" runat="server"
Width="136px">Added Row Data</asp:Label>
<asp:Label id="Label4" style="Z-INDEX: 110; LEFT: 712px; POSITION: absolute; TOP: 144px" runat="server"
Width="136px">Edited Row Data</asp:Label>
<asp:TextBox id="TextBox1" style="Z-INDEX: 111; LEFT: 656px; POSITION: absolute; TOP: 512px"
runat="server" Width="104px" Height="24px"></asp:TextBox>
<asp:Label id="Label5" style="Z-INDEX: 112; LEFT: 272px; POSITION: absolute; TOP: 528px" runat="server"
Width="225px" Height="16px">Enter Employee ID For Search</asp:Label>
</form>
</body>
</HTML>
Application Logic
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace DataGridUpdate
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.WebControls.DataGrid DataGrid3;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
public OleDbDataAdapter odap;
public OleDbConnection Con;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label5;
public DataSet data;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
BindGrid();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void BindGrid()
{
Con = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source =c:\robert\Emp.mdb");
odap = new OleDbDataAdapter("select * from emp", Con);
data = new DataSet();
odap.Fill(data, "emp");
DataGrid1.DataSource = data.Tables[0];
DataGrid1.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
DataTable table = data.Tables[0];
DataRow row = null;
row = table.NewRow();
row[0] = 65;
row[1] = "Kumar";
row[2] = 125655;
table.Rows.Add(row);
DataGrid2.DataSource = table;
DataGrid2.DataBind();
}
private void Button2_Click(object sender, System.EventArgs e)
{
DataTable table = data.Tables[0];
string Qstr = "empid ='" + TextBox1.Text.Trim() + "'";
DataRow[] row = table.Select(Qstr);
foreach (DataRow r in row)
{
// Iam Modifying only the retrieved Content this is automatically reflected in the DataSource table;
r[1] = "Ajith";
r[2] = 787879;
}
DataGrid3.DataSource = table;
DataGrid3.DataBind();
}
}
}
Presentation Logic
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridUpdate.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 600px" runat="server"
Text="AddNewRow" Width="128px"></asp:Button>
<asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 600px"
runat="server" Text="Edit Row" Width="136px"></asp:Button>
<asp:Button id="Button3" style="Z-INDEX: 103; LEFT: 504px; POSITION: absolute; TOP: 600px"
runat="server" Text="Refresh" Width="144px"></asp:Button>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 96px; POSITION: absolute; TOP: 192px"
runat="server"></asp:DataGrid>
<asp:DataGrid id="DataGrid2" style="Z-INDEX: 105; LEFT: 408px; POSITION: absolute; TOP: 192px"
runat="server"></asp:DataGrid>
<asp:DataGrid id="DataGrid3" style="Z-INDEX: 106; LEFT: 704px; POSITION: absolute; TOP: 200px"
runat="server"></asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 368px; POSITION: absolute; TOP: 16px" runat="server"
Width="408px" Height="32px" Font-Size="Medium">Implementing DataGrid Action From Application
Logic</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 108; LEFT: 112px; POSITION: absolute; TOP: 136px" runat="server"
Width="147px">Existing Data</asp:Label>
<asp:Label id="Label3" style="Z-INDEX: 109; LEFT: 416px; POSITION: absolute; TOP: 144px" runat="server"
Width="136px">Added Row Data</asp:Label>
<asp:Label id="Label4" style="Z-INDEX: 110; LEFT: 712px; POSITION: absolute; TOP: 144px" runat="server"
Width="136px">Edited Row Data</asp:Label>
<asp:TextBox id="TextBox1" style="Z-INDEX: 111; LEFT: 656px; POSITION: absolute; TOP: 512px"
runat="server" Width="104px" Height="24px"></asp:TextBox>
<asp:Label id="Label5" style="Z-INDEX: 112; LEFT: 272px; POSITION: absolute; TOP: 528px" runat="server"
Width="225px" Height="16px">Enter Employee ID For Search</asp:Label>
</form>
</body>
</HTML>