TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Itsme
NA
107
5.4k
Delete image from folder
Jul 24 2019 10:28 PM
I am trying to perform curd operation ...everything is working fine but when I delete a entry from gridview ...it is being deleted successfully but it is not being deleted from folder ..where all the images are beingstored.Actually i dont have file name so how can i delete image from folder please suggest.
designer page:
<
asp:GridView
runat
=
"server"
ID
=
"gvImage"
AutoGenerateColumns
=
"false"
CssClass
=
"mydatagrid"
OnRowCancelingEdit
=
"gvImage_RowCancelingEdit"
DataKeyNames
=
"Id"
CellPadding
=
"4"
OnRowEditing
=
"gvImage_RowEditing"
OnRowUpdating
=
"gvImage_RowUpdating"
OnRowDeleting
=
"gvImage_RowDeleting"
HeaderStyle-BackColor
=
"Black"
HeaderStyle-ForeColor
=
"White"
>
<
Columns
>
<
asp:TemplateField
HeaderText
=
"Id"
HeaderStyle-Width
=
"70px"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblId"
runat
=
"server"
Text
=
'<%#Container.DataItemIndex+1%>'
>
</
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Business Name"
HeaderStyle-Width
=
"200px"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblBname"
runat
=
"server"
Text
=
'<%# Eval("Bussiness_name") %>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"txt_BName"
runat
=
"server"
Text
=
'<%# Eval("Bussiness_name") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"City"
HeaderStyle-Width
=
"200px"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblcity"
runat
=
"server"
Text
=
'<%# Eval("city") %>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"txt_city"
runat
=
"server"
Text
=
'<%# Eval("city") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Name"
HeaderStyle-Width
=
"200px"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblname"
runat
=
"server"
Text
=
'<%# Eval("name") %>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"txt_name"
runat
=
"server"
Text
=
'<%# Eval("name") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Toll Free No."
HeaderStyle-Width
=
"200px"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lbltoll"
runat
=
"server"
Text
=
'<%# Eval("toll_free") %>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"txt_toll"
runat
=
"server"
Text
=
'<%# Eval("toll_free") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Website Link"
HeaderStyle-Width
=
"200px"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblweb"
runat
=
"server"
Text
=
'<%# Eval("website_link") %>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"txt_link"
runat
=
"server"
Text
=
'<%# Eval("website_link") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Payment Mode"
HeaderStyle-Width
=
"200px"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblpay"
runat
=
"server"
Text
=
'<%# Eval("payment_mode") %>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"txt_pay"
runat
=
"server"
Text
=
'<%# Eval("payment_mode") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Email"
HeaderStyle-Width
=
"200px"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblmail"
runat
=
"server"
Text
=
'<%# Eval("email") %>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"txt_email"
runat
=
"server"
Text
=
'<%# Eval("email") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Image"
HeaderStyle-Width
=
"200px"
>
<
ItemTemplate
>
<
asp:Image
ID
=
"Image1"
runat
=
"server"
ImageUrl
=
'<%# Eval("Images") %>'
Height
=
"80px"
Width
=
"100px"
/>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:Image
ID
=
"img_user"
runat
=
"server"
ImageUrl
=
'<%# Eval("Images") %>'
Height
=
"80px"
Width
=
"100px"
/>
<
br
/>
<
asp:FileUpload
ID
=
"FileUpload1"
runat
=
"server"
/>
</
EditItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderStyle-Width
=
"150px"
>
<
ItemTemplate
>
<
asp:LinkButton
ID
=
"lbl_edit"
runat
=
"server"
CommandName
=
"Edit"
>
Edit
</
asp:LinkButton
>
<
asp:LinkButton
ID
=
"lbl_delete"
runat
=
"server"
CommandName
=
"Delete"
>
Delete
</
asp:LinkButton
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:LinkButton
ID
=
"lbl_update"
runat
=
"server"
CommandName
=
"Update"
>
Update
</
asp:LinkButton
>
<
asp:LinkButton
ID
=
"lbl_cancle"
runat
=
"server"
CommandName
=
"Cancel"
>
Cancel
</
asp:LinkButton
>
</
EditItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
</
asp:GridView
>
code behind:
namespace
Jobopedia
{
public
partial
class
SP_edit : System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
SqlCommand cmd;
string
constr = ConfigurationManager.ConnectionStrings[
"Q_SS_regnConnectionString1"
].ConnectionString;
string
path =
"~/uploads/"
;
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!
this
.IsPostBack)
{
//this.BindGrid();
ImageData();
}
}
protected
void
ImageData()
{
con =
new
SqlConnection(constr);
con.Open();
da =
new
SqlDataAdapter(
"select * from Q_SP_Afterlogin"
, con);
ds =
new
DataSet();
da.Fill(ds);
gvImage.DataSource = ds;
gvImage.DataBind();
}
protected
void
gvImage_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
{
gvImage.EditIndex = -1;
ImageData();
}
protected
void
gvImage_RowEditing(
object
sender, GridViewEditEventArgs e)
{
gvImage.EditIndex = e.NewEditIndex;
ImageData();
}
protected
void
gvImage_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
{
//find image id of edit row
string
Id = gvImage.DataKeys[e.RowIndex].Value.ToString();
// find values for update
TextBox txt_BName = (TextBox)gvImage.Rows[e.RowIndex].FindControl(
"txt_BName"
);
TextBox txt_city = (TextBox)gvImage.Rows[e.RowIndex].FindControl(
"txt_city"
);
TextBox txt_name = (TextBox)gvImage.Rows[e.RowIndex].FindControl(
"txt_name"
);
TextBox txt_toll = (TextBox)gvImage.Rows[e.RowIndex].FindControl(
"txt_toll"
);
TextBox txt_link = (TextBox)gvImage.Rows[e.RowIndex].FindControl(
"txt_link"
);
TextBox txt_pay = (TextBox)gvImage.Rows[e.RowIndex].FindControl(
"txt_pay"
);
TextBox txt_email = (TextBox)gvImage.Rows[e.RowIndex].FindControl(
"txt_email"
);
FileUpload FileUpload1 = (FileUpload)gvImage.Rows[e.RowIndex].FindControl(
"FileUpload1"
);
con =
new
SqlConnection(constr);
string
path =
"~/uploads/"
;
if
(FileUpload1.HasFile)
{
path += FileUpload1.FileName;
//save image in folder
FileUpload1.SaveAs(MapPath(path));
}
else
{
// use previous user image if new image is not changed
Image img = (Image)gvImage.Rows[e.RowIndex].FindControl(
"img_user"
);
path = img.ImageUrl;
}
SqlCommand cmd =
new
SqlCommand(
"update Q_SP_Afterlogin set Bussiness_name='"
+ txt_BName.Text +
"',city='"
+ txt_city.Text +
"',name='"
+ txt_name.Text +
"',toll_free='"
+ txt_toll.Text +
"',website_link='"
+ txt_link.Text +
"',payment_mode='"
+ txt_pay.Text +
"',email='"
+ txt_email.Text +
"',Images='"
+ path +
"' where Id="
+ Id +
""
, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
gvImage.EditIndex = -1;
ImageData();
}
protected
void
gvImage_RowDeleting(
object
sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)gvImage.Rows[e.RowIndex];
Label Id = (Label)row.FindControl(
"lblId"
);
Label Bussiness_name = (Label)row.FindControl(
"lblBname"
);
Label city = (Label)row.FindControl(
"lblcity"
);
Label name = (Label)row.FindControl(
"lblname"
);
Label toll_free = (Label)row.FindControl(
"lbltoll"
);
Label website_link = (Label)row.FindControl(
"lblweb"
);
Label payment_mode = (Label)row.FindControl(
"lblpay"
);
Label email = (Label)row.FindControl(
"lblmail"
);
//Image Images = (Image)row.FindControl("Image1");
con =
new
SqlConnection(constr);
con.Open();
SqlCommand cmd =
new
SqlCommand(
"delete FROM Q_SP_Afterlogin where Id='"
+ Convert.ToInt32(gvImage.DataKeys[e.RowIndex].Value.ToString()) +
"'"
, con);
cmd.ExecuteNonQuery();
con.Close();
ImageDeleteFromFolder(path);
// path is folder path
ImageData();
}
protected
void
ImageDeleteFromFolder(
string
Images)
/* In Images path is being stored */
{
string
file_name = Images;
string
path = Server.MapPath(
"~/uploads/"
);
string
filePath = path + file_name;
FileInfo file =
new
FileInfo(filePath);
if
(file.Exists)
//check file exsit or not
{
file.Delete();
lblResult.Text =
" file deleted successfully"
;
lblResult.ForeColor = System.Drawing.Color.Green;
}
else
{
lblResult.Text =
" This file does not exists "
;
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
}
}
Attachment:
code.rar
Reply
Answers (
11
)
HIghcharts in asp,net application
How to find control in datagrid in wpf?