This example will help you learn how to display the selected row data of GridView into the TextBoxes using 3-Tier Architecture in ASP.NET.
Step 1
Open Visual Studio and select "ASP.NET Empty Website template". Select the path where you want to create the project.
Step 2
After selecting ASP.NET Empty Web Site template, add a new "class library" project with the name “DAL” in solution explorer.
Step 3
After adding the new "class library" project with the name “DAL”, write the following code.
Namespaces
- using System.Data;
- using System.Data.SqlClient;
code
- publicclassclsDAL {
- SqlConnection con = new SqlConnection("Data Source=DESKTOP-LDDD5B0;Initial Catalog=3TireDB;Integrated Security=True");
- publicvoid InsetData(string _name, string _email, string _address) {
- con.Open();
- SqlDataAdapter adpt = new SqlDataAdapter("Insert into Usertbl(Name,Email,Address) values('" + _name + "','" + _email + "','" + _address + "')", con);
- DataTable dt = new DataTable();
- adpt.Fill(dt);
- con.Close();
- }
- publicvoid DeletData(Int32 _ID) {
- con.Open();
- SqlDataAdapter adpt = new SqlDataAdapter("Delete from Usertbl where ID= " + _ID + "", con);
- DataTable dt = new DataTable();
- adpt.Fill(dt);
- con.Close();
- }
- publicobject SelectData() {
- DataTable dt = new DataTable();
- SqlDataAdapter adpt = new SqlDataAdapter("Select * from Usertbl ", con);
- adpt.Fill(dt);
- return dt;
- }
- publicvoid hfData(Int32 _hfd) {
- DataTable dt = new DataTable();
- if (dt.Rows.Count > 0) {
- SqlDataAdapter adpt = new SqlDataAdapter("Select (ID) from Usertbl values(" + _hfd + ")", con);
- adpt.Fill(dt);
- }
- }
- publicvoid UpdData(string _name, string _email, string _address, Int32 _Id) {
- con.Open();
- SqlDataAdapter adpt = new SqlDataAdapter("Update Usertbl set [Name]='" + _name + "',[Email]='" + _email + "',[Address]='" + _address + "' where [ID]=" + _Id + "", con);
- DataTable dt = new DataTable();
- adpt.Fill(dt);
- con.Close();
- }
- }
Step 4
Now, add one more “Class Libray ” with the name “BLL” to the solution explorer to implement Business Logic Layer.
Step 5
After adding BLL, add the reference of “DAL” to it.
Step 6
After adding the reference to BLL, write the following code.
Namespaces
using DAL;
code
- publicclasscslBLL {
- clsDAL DALobj = newclsDAL();
- publicvoid Insertvalues(string _Nam, string _email, string _address) {
- DALobj.InsetData(_Nam, _email, _address);
- }
- publicvoid Deletevalues(Int32 _id) {
- DALobj.DeletData(_id);
- }
- publicobject SelectValues() {
- return DALobj.SelectData();
- }
- publicvoid hfDataID(Int32 _id) {
- DALobj.hfData(_id);
- }
- publicvoid UpdateValues(string _name, string _email, string _address, Int32 _Id) {
- DALobj.UpdData(_name, _email, _address, _Id);
- }
- }
Step 5
Now, add a new web form with the name ”Index.aspx” to your solution explorer’s Empty website templete.
Step 6
Now, design your “Index.asp” page using the following code.
Index.aspx
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Indext.aspx.cs"Inherits="Indext"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml"> <headid="Head1" runat="server">
- <title></title>
- <styletype="text/css"> .style1 { width: 71%; } .style2 { font-size: large; color: #000046; } </style> </head> <body>
- <formid="form1"runat="server">
- <tablealign="center"class="style1">
- <tr>
- <td> <spanclass="style2"><strong>3 - Tire Architecture</strong></span> </td>
- </tr>
- <tr>
- <td>
- <asp:HiddenFieldID="HiddenField1"runat="server" />
- </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td> <asp:LabelID="Label1"runat="server"Text="Name">
- </asp:Label> <asp:TextBoxID="TextBox1"runat="server"Width="222px"Style="margin-left: 5px">
- </asp:TextBox> <asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ErrorMessage="Name is Reqired" ControlToValidate="TextBox1" ForeColor="Red">
- </asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td> <asp:LabelID="Label2"runat="server"Text="Emmail">
- </asp:Label> <asp:TextBoxID="TextBox2"runat="server"Width="222px"Style="margin-left: 0px">
- </asp:TextBox> <asp:RequiredFieldValidatorID="RequiredFieldValidator2"runat="server"ErrorMessage="Email is Required" ControlToValidate="TextBox2" ForeColor="Red">
- </asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td> <asp:LabelID="Label3"runat="server"Text="Address">
- </asp:Label> <asp:TextBoxID="TextBox3"runat="server"Width="222px"Style="margin-left: 0px">
- </asp:TextBox> <asp:RequiredFieldValidatorID="RequiredFieldValidator3"runat="server"ErrorMessage="Address is Required" ControlToValidate="TextBox3" ForeColor="Red">
- </asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>
- <asp:ButtonID="Savebtn"runat="server"Text="Save"Width="62px"OnClick="Savebtn_Click" Style="margin-left: 5px" />
- <asp:ButtonID="Updatebtn"runat="server"Text="Update"Width="63px"OnClick="Updatebtn_Click" Style="margin-left: 18px" />
- <asp:ButtonID="Delete"runat="server"Style="text-align: left; margin-left: 15px;" Text="Delete" Width="58px" OnClick="Delete_Click" />
- </td>
- </tr>
- <tr>
- <tdstyle="text-align: left"> <asp:Label ID="Label4" runat="server" Style="text-align: center" Text="Label" Font-Bold="True" Font-Size="Large" ForeColor="#009900"></asp:Label>
- </td>
- </tr>
- <tr>
- <td> <asp:GridViewID="GridView1"runat="server"AutoGenerateSelectButton="True"OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" Style="margin-left: 265px;
-
- margin-top: -16px" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">
- <Columns>
- <asp:BoundFieldDataField="ID"HeaderText="ID"ReadOnly="True"SortExpression="ID" />
- <asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name" />
- <asp:BoundFieldDataField="Email"HeaderText="Email"SortExpression="Email" />
- <asp:BoundFieldDataField="Address"HeaderText="Address"SortExpression="Address" />
- </Columns>
- <FooterStyleBackColor="#99CCCC"ForeColor="#003399" />
- <HeaderStyleBackColor="#003399"Font-Bold="True"ForeColor="#CCCCFF" />
- <PagerStyleBackColor="#99CCCC"ForeColor="#003399"HorizontalAlign="Left" />
- <RowStyleBackColor="White"ForeColor="#003399" />
- <SelectedRowStyleBackColor="#009999"ForeColor="#CCFF99"Font-Bold="true" />
- <SortedAscendingCellStyleBackColor="#EDF6F6" />
- <SortedAscendingHeaderStyleBackColor="#0D4AC4" />
- <SortedDescendingCellStyleBackColor="#D6DFDF" />
- <SortedDescendingHeaderStyleBackColor="#002876" />
- </asp:GridView>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Step 7
Now, add a reference to BLL to your solution explorer’s empty website template. Write the following code on the “Index.aspx.cs” page.
Index.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using BLL;
- publicpartialclassIndext: System.Web.UI.Page {
- protectedvoid Page_Load(object sender, EventArgs e) {
- Label4.Visible = false;
- }
- protectedvoid Savebtn_Click(object sender, EventArgs e) {
- cslBLL clsBLLobj = newcslBLL();
- clsBLLobj.Insertvalues(TextBox1.Text, TextBox2.Text, TextBox3.Text);
- GridView1.DataSource = clsBLLobj.SelectValues();
- GridView1.DataBind();
- Label4.Visible = true;
- Label4.Text = "Data Insered";
- }
- protectedvoid Updatebtn_Click(object sender, EventArgs e) {
- cslBLL clsBLLobj = newcslBLL();
- clsBLLobj.UpdateValues(TextBox1.Text, TextBox2.Text, TextBox3.Text, Convert.ToInt32(HiddenField1.Value));
- GridView1.DataSource = clsBLLobj.SelectValues();
- GridView1.DataBind();
- Label4.Visible = true;
- Label4.Text = "Record Updated";
- }
- protectedvoid Delete_Click(object sender, EventArgs e) {
- cslBLL clsBLLobj = newcslBLL();
- clsBLLobj.Deletevalues(Convert.ToInt32(HiddenField1.Value));
- GridView1.DataSource = clsBLLobj.SelectValues();
- GridView1.DataBind();
- Label4.Visible = true;
- Label4.Text = "Record Deleted";
- }
- protectedvoid GridView1_SelectedIndexChanged(object sender, EventArgs e) {
- GridViewRow row = GridView1.SelectedRow;
- HiddenField1.Value = row.Cells[1].Text;
- TextBox1.Text = row.Cells[2].Text;
- TextBox2.Text = row.Cells[3].Text;
- TextBox3.Text = row.Cells[4].Text;
- }
- protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
- if (e.Row.RowType == DataControlRowType.Header) {
- e.Row.Cells[0].Style.Add(HtmlTextWriterStyle.Display, "none");
- }
- if (e.Row.RowType == DataControlRowType.DataRow) {
- e.Row.Cells[0].Style.Add(HtmlTextWriterStyle.Display, "none");
- e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
- }
- }
- }