Create Table
create table news_info(imageid
int primary key identity(1,1)
,Title varchar(100),
name varchar(100),
discription varchar(max),
ImagePath varchar(max))
Create Store Procedure
-----create
procedure for Insert image-----------
CREATE PROCEDURE Savenews_Sp
(
@Title varchar(100),
@name varchar(100),
@discription varchar(max),
@ImagePath varchar(500)
)
AS
BEGIN
insert into news_info(Title,name,discription,ImagePath)
values (@Title,@name,@discription,@ImagePath)
END
------create
procedure for show image-----------
create PROCEDURE shownews_Sp
AS
BEGIN
select
* from
news_info
END
------create
procedure for update image-----------
Alter PROCEDURE updatenews_Sp
( @imageid int,
@Title varchar(100),
@name varchar(100),
@discription varchar(max)
)
AS
BEGIN
update
news_info set Title=@Title,name=@name,discription=@discription
where imageid=@imageid
END
------create
procedure for delete image--------------
CREATE PROCEDURE Deletenews_Sp
(
@imageid int
)
AS
BEGIN
delete
from news_info where
imageid=@imageid
END
edit_update_delete_show_image.aspx
<% Page
Language="C#"AutoEventWireup="true"
CodeFile="edit_update_delete_show_image.aspx.cs"
Inherits="edit_update_delete_show_image"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<fieldset style="width:550px">
<legend>Bind,Save,Edit,Update,Delete in DataList</legend>
<table style="width:100%">
<tr><td width="25%">Title</td><td>
<asp:TextBox ID="txtTitle"
runat="server"></asp:TextBox></td></tr>
<tr><td>Name</td><td>
<asp:TextBox ID="txtname"
runat="server"></asp:TextBox></td></tr>
<tr><td>Discription</td><td>
<asp:TextBox ID="txtdiscription"
runat="server"></asp:TextBox></td></tr>
<tr><td>Upload Image</td><td>
<asp:FileUpload ID="FileUpload1"
runat="server"
/></td></tr>
<tr><td></td>
<td> <asp:Button ID="btnSubmit" runat="server" Text="Save"
onclick="btnSubmit_Click"
/></td></tr>
</table>
<asp:DataList ID="Datalist_news"
runat="server"
RepeatColumns="2"
oncancelcommand="dtlNews_CancelCommand"
oneditcommand="dtlNews_EditCommand"
onupdatecommand="dtlNews_UpdateCommand"
ondeletecommand="dtlNews_DeleteCommand"
DataKeyField="imageid"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
CellPadding="4"
GridLines="Both"
onselectedindexchanged="btnSubmit_Click">
<FooterStyle BackColor="#99CCCC"
ForeColor="#003399"
/>
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<ItemStyle BackColor="White"
ForeColor="#003399"
/>
<ItemTemplate>
<table>
<tr>
<td><img src='<%#Eval("ImagePath") %>' width="90px"
height="90px"
alt="Book
Image" /></td><td><b>Title : </b><%#Eval("Title")%><br />
<b>Name : </b><%#Eval("name")%><br />
<b>Discription : </b><%#Eval("discription")%><br />
</tr>
</table>
<asp:LinkButton ID="lnkEdit"
runat="server"
Text="Edit"
CommandName="Edit"></asp:LinkButton>
<asp:LinkButton ID="lnkDelete"
runat="server"
Text="Delete"
CommandName="Delete"
OnClientClick="return
confirm('Are you sure you want to delete selected records')"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<b>Title : </b><asp:TextBox id="txtEditTitle" runat="server" Text='<%#Eval("Title") %>'></asp:TextBox><br />
<b>Name : </b><asp:TextBox id="txtEditname" runat="server" Text='<%#Eval("name") %>'></asp:TextBox><br />
<b>Discription : </b><asp:TextBox id="txtEditdiscription" runat="server"
Text='<%#Eval("discription") %>'></asp:TextBox><br />
<asp:LinkButton ID="lnkUpdate"
runat="server"
Text="Update"
CommandName="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel"
runat="server"
Text="Cancel"
CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
<SelectedItemStyle
BackColor="#009999"
Font-Bold="True"
ForeColor="#CCFF99"
/>
</asp:DataList>
</fieldset>
</form>
</body>
</html>
edit_update_delete_show_image.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class edit_update_delete_show_image
: System.Web.UI.Page
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void Page_Load(object
sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDataList();
}
}
protected void
BindDataList()
{
SqlDataAdapter adp = new
SqlDataAdapter();
DataTable dt = new
DataTable();
try
{
adp = new SqlDataAdapter("shownews_Sp", con);
adp.SelectCommand.CommandType = CommandType.StoredProcedure;
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
Datalist_news.DataSource = dt;
Datalist_news.DataBind();
}
else
{
Datalist_news.DataSource = dt;
Datalist_news.DataBind();
}
}
catch (Exception
ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Oops!! Error occured : " +
ex.Message.ToString() + "');", true);
}
finally
{
con.Close();
dt.Clear();
dt.Dispose();
}
}
protected void
btnSubmit_Click(object sender, EventArgs e)
{
string filePath = string.Empty;
SqlCommand cmd = new
SqlCommand();
string ImgPath = string.Empty;
string DbImgPath = string.Empty;
try
{
cmd = new SqlCommand("SaveNews_Sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Title",
txtTitle.Text.Trim());
cmd.Parameters.AddWithValue("@name",
txtname.Text.Trim());
cmd.Parameters.AddWithValue("@discription",
txtdiscription.Text.Trim());
if (FileUpload1.HasFile)
{
ImgPath = (Server.MapPath("~/BookImages/")
+ Guid.NewGuid() + FileUpload1.FileName);
FileUpload1.SaveAs(ImgPath);
DbImgPath = ImgPath.Substring(ImgPath.LastIndexOf("\\"));
DbImgPath = DbImgPath.Insert(0, "BookImages");
cmd.Parameters.AddWithValue("@ImagePath", DbImgPath);
}
else
{
cmd.Parameters.AddWithValue("@ImagePath",
string.Empty);
}
con.Open();
cmd.ExecuteNonQuery();
BindDataList();
ClearControls();
ScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Book record has been saved successfully');", true);
}
catch (Exception
ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Oops!! Error occured : " +
ex.Message.ToString() + "');", true);
}
finally
{
con.Close();
cmd.Dispose();
}
}
protected void
dtlNews_CancelCommand(object source, DataListCommandEventArgs e)
{
Datalist_news.EditItemIndex = -1;
BindDataList();
}
protected void
dtlNews_DeleteCommand(object source, DataListCommandEventArgs e)
{
Int32 bookId;
SqlCommand cmd = new
SqlCommand();
try
{
bookId = Convert.ToInt32(Datalist_news.DataKeys[e.Item.ItemIndex]);
cmd = new SqlCommand("Deletenews_Sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@imageid",
bookId);
con.Open();
cmd.ExecuteNonQuery();
BindDataList();
ScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Book record has been deleted successfully');",
true);
}
catch (Exception
ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Oops!! Error occured : " +
ex.Message.ToString() + "');", true);
}
finally
{
con.Close();
cmd.Dispose();
}
}
protected void
dtlNews_EditCommand(object source, DataListCommandEventArgs e)
{
Datalist_news.EditItemIndex = e.Item.ItemIndex;
BindDataList();
}
protected void
dtlNews_UpdateCommand(object source, DataListCommandEventArgs e)
{
Int32 imageid;
string tit = string.Empty;
string name = string.Empty;
string dis = string.Empty;
SqlCommand cmd = new
SqlCommand();
try
{
tit = ((TextBox)(e.Item.FindControl("txtEditTitle"))).Text;
name = ((TextBox)(e.Item.FindControl("txtEditname"))).Text;
dis = ((TextBox)(e.Item.FindControl("txtEditdiscription"))).Text;
imageid = Convert.ToInt32(Datalist_news.DataKeys[e.Item.ItemIndex]);
con.Open();
cmd = new SqlCommand("updatenews_Sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@imageid",
imageid);
cmd.Parameters.AddWithValue("@Title",
tit);
cmd.Parameters.AddWithValue("@name",
name);
cmd.Parameters.AddWithValue("@discription",dis);
cmd.ExecuteNonQuery();
Datalist_news.EditItemIndex = -1;
BindDataList();
ScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Book record has been updated successfully');",
true);
}
catch (Exception
ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(),
"Message", "alert('Oops!! Error occured : " +
ex.Message.ToString() + "');", true);
}
finally
{
cmd.Dispose();
con.Close();
tit = string.Empty;
name = string.Empty;
dis = string.Empty;
}
}
private void
ClearControls()
{
txtTitle.Text = string.Empty;
txtname.Text = string.Empty;
txtdiscription.Text = string.Empty;
txtTitle.Focus();
}
}
No comments:
Post a Comment