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. Text="AddNewRow" Width="128px"></asp:Button>
  15. <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 600px"
  16. runat="server"
  17. Text="Edit Row" Width="136px"></asp:Button>
  18. <asp:Button id="Button3" style="Z-INDEX: 103; LEFT: 504px; POSITION: absolute; TOP: 600px"
  19. runat="server"
  20. Text="Refresh" Width="144px"></asp:Button>
  21. <asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 96px; POSITION: absolute; TOP: 192px"
  22. runat="server"></asp:DataGrid>
  23. <asp:DataGrid id="DataGrid2" style="Z-INDEX: 105; LEFT: 408px; POSITION: absolute; TOP: 192px"
  24. runat="server"></asp:DataGrid>
  25. <asp:DataGrid id="DataGrid3" style="Z-INDEX: 106; LEFT: 704px; POSITION: absolute; TOP: 200px"
  26. runat="server"></asp:DataGrid>
  27. <asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 368px; POSITION: absolute; TOP: 16px" runat="server"
  28. Width="408px" Height="32px" Font-Size="Medium">Implementing DataGrid Action From Application
  29. Logic</asp:Label>
  30. <asp:Label id="Label2" style="Z-INDEX: 108; LEFT: 112px; POSITION: absolute; TOP: 136px" runat="server"
  31. Width="147px">Existing Data</asp:Label>
  32. <asp:Label id="Label3" style="Z-INDEX: 109; LEFT: 416px; POSITION: absolute; TOP: 144px" runat="server"
  33. Width="136px">Added Row Data</asp:Label>
  34. <asp:Label id="Label4" style="Z-INDEX: 110; LEFT: 712px; POSITION: absolute; TOP: 144px" runat="server"
  35. Width="136px">Edited Row Data</asp:Label>
  36. <asp:TextBox id="TextBox1" style="Z-INDEX: 111; LEFT: 656px; POSITION: absolute; TOP: 512px"
  37. runat="server" Width="104px" Height="24px"></asp:TextBox>
  38. <asp:Label id="Label5" style="Z-INDEX: 112; LEFT: 272px; POSITION: absolute; TOP: 528px" runat="server"
  39. Width="225px" Height="16px">Enter Employee ID For Search</asp:Label>
  40. </form>
  41. </body>
  42. </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. Text="AddNewRow" Width="128px"></asp:Button>
  15. <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 600px"
  16. runat="server" Text="Edit Row" Width="136px"></asp:Button>
  17. <asp:Button id="Button3" style="Z-INDEX: 103; LEFT: 504px; POSITION: absolute; TOP: 600px"
  18. runat="server" Text="Refresh" Width="144px"></asp:Button>
  19. <asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 96px; POSITION: absolute; TOP: 192px"
  20. runat="server"></asp:DataGrid>
  21. <asp:DataGrid id="DataGrid2" style="Z-INDEX: 105; LEFT: 408px; POSITION: absolute; TOP: 192px"
  22. runat="server"></asp:DataGrid>
  23. <asp:DataGrid id="DataGrid3" style="Z-INDEX: 106; LEFT: 704px; POSITION: absolute; TOP: 200px"
  24. runat="server"></asp:DataGrid>
  25. <asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 368px; POSITION: absolute; TOP: 16px" runat="server"
  26. Width="408px" Height="32px" Font-Size="Medium">Implementing DataGrid Action From Application
  27. Logic</asp:Label>
  28. <asp:Label id="Label2" style="Z-INDEX: 108; LEFT: 112px; POSITION: absolute; TOP: 136px" runat="server"
  29. Width="147px">Existing Data</asp:Label>
  30. <asp:Label id="Label3" style="Z-INDEX: 109; LEFT: 416px; POSITION: absolute; TOP: 144px" runat="server"
  31. Width="136px">Added Row Data</asp:Label>
  32. <asp:Label id="Label4" style="Z-INDEX: 110; LEFT: 712px; POSITION: absolute; TOP: 144px" runat="server"
  33. Width="136px">Edited Row Data</asp:Label>
  34. <asp:TextBox id="TextBox1" style="Z-INDEX: 111; LEFT: 656px; POSITION: absolute; TOP: 512px"
  35. runat="server" Width="104px" Height="24px"></asp:TextBox>
  36. <asp:Label id="Label5" style="Z-INDEX: 112; LEFT: 272px; POSITION: absolute; TOP: 528px" runat="server"
  37. Width="225px" Height="16px">Enter Employee ID For Search</asp:Label>
  38. </form>
  39. </body>
  40. </HTML>