Agenda :
Dear all In this article, we will discuss CURD(Create Update Retrieve Delete) Operations with Data List Using asp.net
Description :
In previous articles, I have explained CURD (Create Update Retrieve Delete) Operations Using form view in asp.net , Must Avoid Mistakes using Date Time in C#, MS Sql Server Row_Number() Interview Question 2023, How to work with File upload control using Asp.Net – PART - I, Working with File Upload control using Asp.Net – PART - II, How to resolve maximum requested length exceeded error with file upload control – PART - III different articles related to C#.Net,Asp.Net, SQL Server, XML, and DotNetInterviewQuestions Related articles. Now Let's work on CURD(Create Update Retrieve Delete) Operations with Data List Using asp.net
Steps to work with this:
- Go To Visual Studio Click on New Website
- ClickOnAsp.Net Empty Website(Or choose any web template)Click Ok
- Drag and drop the Data List control
- Implement the code as per the demonstration
CREATE TABLE tbl_Departments(ID INT IDENTITY,
Code VARCHAR(50),Name VARCHAR(100),Location VARCHAR(100),IsACtive BIT,
CreatedOn DATETIME,CreatedBy VARCHAR(100),ModifiedOn DATETIME,ModifiedBy VARCHAR(100))
INSERT INTO tbl_Departments(Code,Name,Location,IsACtive,CreatedBy,CreatedOn) VALUES('TECH','Technical','Hyderabad',1,'Admin',GETDATE())
SELECT *FROM tbl_Departments
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="P1.aspx.cs" Inherits="DataList_Ex.P1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="auto-style1">
<tr>
<td>Enter Code</td>
<td>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Enter Name</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Enter Location</td>
<td>
<asp:TextBox ID="txtLocation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
</td>
<td>
<asp:Button ID="btnDisplay" runat="server" OnClick="btnDisplay_Click" Text="Display" />
</td>
</tr>
<tr>
<td>
<asp:DataList ID="dlDepartments" DataKeyField="Id" runat="server" OnCancelCommand="dlDepartments_CancelCommand" OnDeleteCommand="dlDepartments_DeleteCommand" OnEditCommand="dlDepartments_EditCommand" OnUpdateCommand="dlDepartments_UpdateCommand" RepeatDirection="Horizontal">
<ItemTemplate>
<table border="1" align="Center" width="100%">
<tr>
<td>Code : </td>
<td><%#Eval("Code") %> </td>
</tr>
<tr>
<td>Name : </td>
<td><%#Eval("Name") %> </td>
</tr>
<tr>
<td>Location : </td>
<td><%#Eval("Location") %> </td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure to delete this
record')"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemStyle ForeColor="DarkRed" />
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
<EditItemTemplate>
<table border="1" align="Center" width="100%">
<tr>
<td>Code : </td>
<td>
<asp:TextBox ID="txtECode" runat="server" Text=' <%#Eval("Code") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>Name : </td>
<td>
<asp:TextBox ID="txtEName" runat="server" Text=' <%#Eval("Name") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>Location : </td>
<td><asp:TextBox ID="txtELocation" runat="server" Text=' <%#Eval("Location") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel" Text="Cancel" OnClientClick="return confirm('Are you sure to delete this
record')"></asp:LinkButton>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>
</td>
<td> </td>
</tr>
</table>
<div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DataList_Ex
{
public partial class P1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["Con"]));
SqlDataAdapter da;
DataSet ds;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
}
public void BindData()
{
try
{
da = new SqlDataAdapter("Select *from tbl_Departments", con);
ds = new DataSet();
da.Fill(ds);
dlDepartments.DataSource = ds;
dlDepartments.DataBind();
}
catch (Exception ex)
{
Response.Write("<script>alert(" + ex.Message + ")</script>");
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("INSERT INTO
tbl_Departments(Code,Name,Location,IsACtive,CreatedBy,CreatedOn) VALUES('" + txtCode.Text + "','" + txtName.Text + "','" + txtLocation.Text + "','1','Admin',GETDATE())", con);
int res = cmd.ExecuteNonQuery();
con.Close();
if (res > 0)
{
Response.Write("<script>alert('Data Inserted
Successfully...')</script>");
}
else
{
Response.Write("<script>alert('Data Not Inserted.Please try
again after some time...')</script>");
}
BindData();
}
protected void btnDisplay_Click(object sender, EventArgs e)
{
BindData();
}
protected void dlDepartments_EditCommand(object source, DataListCommandEventArgs e)
{
dlDepartments.EditItemIndex =
e.Item.ItemIndex;
BindData();
}
protected void dlDepartments_CancelCommand(object source, DataListCommandEventArgs
e)
{
dlDepartments.EditItemIndex = -1;
BindData();
}
protected void dlDepartments_UpdateCommand(object source, DataListCommandEventArgs
e)
{
int id;
string code;
string Name;
string Location;
id = Convert.ToInt32(dlDepartments.DataKeys[e.Item.ItemIndex]);
code = ((TextBox)(e.Item.FindControl("txtECode"))).Text;
Name = ((TextBox)(e.Item.FindControl("txtEName"))).Text;
Location = ((TextBox)(e.Item.FindControl("txtELocation"))).Text;
con.Open();
cmd = new SqlCommand("UPDATE tbl_Departments SET Code='" + code + "',Name='" + Name + "',Location='" + Location + "',ModifiedBy='Admin',ModifiedOn=GETDATE()
WHERE Id='" + id + "'", con);
int res = cmd.ExecuteNonQuery();
con.Close();
if (res > 0)
{
Response.Write("<script>alert('Data Updated Successfully...')</script>");
}
else
{
Response.Write("<script>alert('Data Not Updated.Please try
again after some time...')</script>");
}
dlDepartments.EditItemIndex = -1;
BindData();
}
protected void dlDepartments_DeleteCommand(object source, DataListCommandEventArgs
e)
{
int Id = Convert.ToInt32(dlDepartments.DataKeys[e.Item.ItemIndex]);
con.Open();
cmd = new SqlCommand("Delete from tbl_Departments Where Id='" + Id + "'", con);
int res = cmd.ExecuteNonQuery();
con.Close();
if (res > 0)
{
Response.Write("<script>alert('Data Deleted Successfully...')</script>");
}
else
{
Response.Write("<script>alert('Data Not Deleted.Please try
again after some time...')</script>");
}
BindData();
}
}
}
No comments :
Post a Comment