ASP Example
Design proper user interface & create ASP.Net page to perform all database
operations on “books” table. [ Insert, Edit (GridView & Separate Page), List
(Gridview, DataList), Search (Repeater), Delete(GridView & Separate
Page)] Note: use proper validation control.and all in master paging code
Insert Update Delete Edit In DataList Controls
ASPX File
<Code>
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="book.aspx.cs" Inherits="book" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:DataList ID="DataList1" DataKeyField="Id" ShowFooter="true" runat="server" OnCancelCommand="DataList1_CancelCommand" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand" OnDeleteCommand="DataList1_DeleteCommand" OnItemCommand="DataList1_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Auther</th>
<th>Price</th>
<th>operation</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Id") %></td>
<td><%#Eval("name") %></td>
<td><%#Eval("Auther") %></td>
<td><%#Eval("Price") %></td>
<td>
<asp:LinkButton ID="lbledit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="lbldel" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td><%#Eval("Id") %></td>
<td>
<asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</td>
<td><asp:TextBox ID="txtauther" runat="server" Text='<%#Eval("Auther") %>'></asp:TextBox>
</td>
<td> <asp:TextBox ID="txtprice" runat="server" Text='<%#Eval("Price") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lblup" runat="server" CommandName="Update">Update</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
<FooterTemplate>
<tr>
<td colspan="2">
<asp:TextBox ID="txtnameadd" runat="server" ></asp:TextBox>
</td>
<td><asp:TextBox ID="txtautheradd" runat="server" ></asp:TextBox>
</td>
<td> <asp:TextBox ID="txtpriceadd" runat="server" ></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lbladd" runat="server" CommandName="add">add</asp:LinkButton>
</td>
</tr>
</FooterTemplate>
</asp:DataList>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:DataList ID="DataList1" DataKeyField="Id" ShowFooter="true" runat="server" OnCancelCommand="DataList1_CancelCommand" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand" OnDeleteCommand="DataList1_DeleteCommand" OnItemCommand="DataList1_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Auther</th>
<th>Price</th>
<th>operation</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Id") %></td>
<td><%#Eval("name") %></td>
<td><%#Eval("Auther") %></td>
<td><%#Eval("Price") %></td>
<td>
<asp:LinkButton ID="lbledit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="lbldel" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td><%#Eval("Id") %></td>
<td>
<asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</td>
<td><asp:TextBox ID="txtauther" runat="server" Text='<%#Eval("Auther") %>'></asp:TextBox>
</td>
<td> <asp:TextBox ID="txtprice" runat="server" Text='<%#Eval("Price") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lblup" runat="server" CommandName="Update">Update</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
<FooterTemplate>
<tr>
<td colspan="2">
<asp:TextBox ID="txtnameadd" runat="server" ></asp:TextBox>
</td>
<td><asp:TextBox ID="txtautheradd" runat="server" ></asp:TextBox>
</td>
<td> <asp:TextBox ID="txtpriceadd" runat="server" ></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lbladd" runat="server" CommandName="add">add</asp:LinkButton>
</td>
</tr>
</FooterTemplate>
</asp:DataList>
</asp:Content>
</Code>
ASPX.CS File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class book : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\asp.net\tuto6\App_Data\librarys.mdf;Integrated Security=True");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();
}
}
public void getdata()
{
SqlDataAdapter adpt = new SqlDataAdapter("select * from Books", con);
DataSet ds = new DataSet();
adpt.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
getdata();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
getdata();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
int id = Convert.ToInt16(DataList1.DataKeys[e.Item.ItemIndex].ToString());
TextBox txtname = (TextBox)e.Item.FindControl("txtname");
TextBox txtauther = (TextBox)e.Item.FindControl("txtauther");
TextBox txtprice = (TextBox)e.Item.FindControl("txtprice");
cmd = new SqlCommand("update Books set name=@name,Auther=@auth,Price=@pr where Id='" + id + "' ", con);
cmd.Parameters.AddWithValue("@auth", txtauther.Text);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@pr", txtprice.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
getdata();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
int id = Convert.ToInt16(DataList1.DataKeys[e.Item.ItemIndex].ToString());
cmd = new SqlCommand("delete from Books where Id='" + id + "' ", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
getdata();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "add")
{
TextBox txtnameadd = (TextBox)e.Item.FindControl("txtnameadd");
TextBox txtautheradd = (TextBox)e.Item.FindControl("txtautheradd");
TextBox txtpriceadd = (TextBox)e.Item.FindControl("txtpriceadd");
cmd = new SqlCommand("insert into Books values(@name,@auth,@pr) ", con);
cmd.Parameters.AddWithValue("@auth", txtautheradd.Text);
cmd.Parameters.AddWithValue("@name", txtnameadd.Text);
cmd.Parameters.AddWithValue("@pr", txtpriceadd.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
getdata();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class book : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\asp.net\tuto6\App_Data\librarys.mdf;Integrated Security=True");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();
}
}
public void getdata()
{
SqlDataAdapter adpt = new SqlDataAdapter("select * from Books", con);
DataSet ds = new DataSet();
adpt.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
getdata();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
getdata();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
int id = Convert.ToInt16(DataList1.DataKeys[e.Item.ItemIndex].ToString());
TextBox txtname = (TextBox)e.Item.FindControl("txtname");
TextBox txtauther = (TextBox)e.Item.FindControl("txtauther");
TextBox txtprice = (TextBox)e.Item.FindControl("txtprice");
cmd = new SqlCommand("update Books set name=@name,Auther=@auth,Price=@pr where Id='" + id + "' ", con);
cmd.Parameters.AddWithValue("@auth", txtauther.Text);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@pr", txtprice.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
getdata();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
int id = Convert.ToInt16(DataList1.DataKeys[e.Item.ItemIndex].ToString());
cmd = new SqlCommand("delete from Books where Id='" + id + "' ", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
getdata();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "add")
{
TextBox txtnameadd = (TextBox)e.Item.FindControl("txtnameadd");
TextBox txtautheradd = (TextBox)e.Item.FindControl("txtautheradd");
TextBox txtpriceadd = (TextBox)e.Item.FindControl("txtpriceadd");
cmd = new SqlCommand("insert into Books values(@name,@auth,@pr) ", con);
cmd.Parameters.AddWithValue("@auth", txtautheradd.Text);
cmd.Parameters.AddWithValue("@name", txtnameadd.Text);
cmd.Parameters.AddWithValue("@pr", txtpriceadd.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
getdata();
}
}
}
Searching Data in Database Display Repeater Control
ASPX File
<Code>
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="search.aspx.cs" Inherits="search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Auther</th>
<th>Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Id") %></td>
<td><%#Eval("Name") %></td>
<td><%#Eval("Auther") %></td>
<td><%#Eval("Price") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Auther</th>
<th>Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Id") %></td>
<td><%#Eval("Name") %></td>
<td><%#Eval("Auther") %></td>
<td><%#Eval("Price") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
</Code>
ASPX.CS File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class search : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\asp.net\tuto6\App_Data\librarys.mdf;Integrated Security=True");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataAdapter adpt = new SqlDataAdapter("select * from Books", con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField ="name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt16(DropDownList1.SelectedValue.ToString());
SqlDataAdapter adpt = new SqlDataAdapter("select * from Books where id='"+id+"'",con);
DataSet ds = new DataSet();
adpt.Fill(ds);
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string str = TextBox1.Text;
SqlDataAdapter adpt = new SqlDataAdapter("select * from Books where name like '%" +str + "%'", con);
DataSet ds = new DataSet();
adpt.Fill(ds);
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class search : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\asp.net\tuto6\App_Data\librarys.mdf;Integrated Security=True");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataAdapter adpt = new SqlDataAdapter("select * from Books", con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField ="name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt16(DropDownList1.SelectedValue.ToString());
SqlDataAdapter adpt = new SqlDataAdapter("select * from Books where id='"+id+"'",con);
DataSet ds = new DataSet();
adpt.Fill(ds);
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string str = TextBox1.Text;
SqlDataAdapter adpt = new SqlDataAdapter("select * from Books where name like '%" +str + "%'", con);
DataSet ds = new DataSet();
adpt.Fill(ds);
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
}
}
Comments
Post a Comment