First use gridview :
<asp:GridView ID="grdAcademicResourceFilesView" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="ID" ForeColor="#333333" GridLines="None" OnRowDeleting="grdAcademicResourceFilesView_RowDeleting"
Width="100%">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Name" HeaderText="Category Name" SortExpression="Name" />
<asp:BoundField DataField="ProgramCode" HeaderText="Program Code" SortExpression="ProgramCode" />
<asp:BoundField DataField="Semester" DataFormatString="Semester {0}" HeaderText="Term Id"
SortExpression="Semester" />
<asp:BoundField DataField="UploadedFileName" HeaderText="File Name" SortExpression="UploadedFileName" />
<asp:BoundField DataField="CreatedBy" HeaderText="Uploaded By" SortExpression="CreatedBy" />
<asp:BoundField DataField="CreatedDate" DataFormatString="{0:d}" HeaderText="Upload Date"
SortExpression="CreatedDate" />
<asp:BoundField DataField="CourseCode" HeaderText="Course Code" SortExpression="CourseCode" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks" SortExpression="Remarks" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm ('Are you sure to delete this record');" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
Then add sqldata source to attach to it.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LCODLConnectionString %>"
SelectCommand="LcOdlViewAcademicUploadedFiles" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Then create event of gridview ROW DELETING and paste following code in code behind model
protected void grdAcademicResourceFilesView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string rowID = Convert.ToString(grdAcademicResourceFilesView.DataKeys[e.RowIndex]["ID"]);
string uploadedFileName = grdAcademicResourceFilesView.Rows[e.RowIndex].Cells[4].Text;
if (!string.IsNullOrEmpty(uploadedFileName))
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LCODLConnectionString"].ConnectionString))
{
SqlTransaction tran = null;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
tran = con.BeginTransaction();
bool canDeleteFile = false;
try
{
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@id", rowID);
param[1] = new SqlParameter("@canDelete", SqlDbType.Bit);
param[1].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(tran, CommandType.StoredProcedure, "LcOdlDeleteUploadedAcademicFile", param);
canDeleteFile = Convert.ToBoolean(param[1].Value);
tran.Commit();
}
catch (SqlException ex)
{
tran.Rollback();
ScriptManager.RegisterStartupScript(grdAcademicResourceFilesView, grdAcademicResourceFilesView.GetType(), "DeletingError", "alert('Some thing went wrong while deleting file. Please try again later');", true);
return;
}
finally
{
con.Close();
}
//Now going To Deleet file from filesystem
string Rootfolder = Server.MapPath("~/IpSyllabusSchemas");
string FilePathWithName = string.Format("{0}\\{1}", Rootfolder, uploadedFileName);
if (canDeleteFile)
{
if (File.Exists(FilePathWithName))
{
try
{
File.Delete(FilePathWithName);
}
catch (Exception ex)
{
tran.Rollback();
ScriptManager.RegisterStartupScript(grdAcademicResourceFilesView, grdAcademicResourceFilesView.GetType(), "DeletingError", "alert('Some thing went wrong while deleting file. Please try again later');", true);
return;
}
}
}
grdAcademicResourceFilesView.DataSource = GetUploadedFiles();
grdAcademicResourceFilesView.DataBind();
}
}
}
Done :)
<asp:GridView ID="grdAcademicResourceFilesView" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="ID" ForeColor="#333333" GridLines="None" OnRowDeleting="grdAcademicResourceFilesView_RowDeleting"
Width="100%">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Name" HeaderText="Category Name" SortExpression="Name" />
<asp:BoundField DataField="ProgramCode" HeaderText="Program Code" SortExpression="ProgramCode" />
<asp:BoundField DataField="Semester" DataFormatString="Semester {0}" HeaderText="Term Id"
SortExpression="Semester" />
<asp:BoundField DataField="UploadedFileName" HeaderText="File Name" SortExpression="UploadedFileName" />
<asp:BoundField DataField="CreatedBy" HeaderText="Uploaded By" SortExpression="CreatedBy" />
<asp:BoundField DataField="CreatedDate" DataFormatString="{0:d}" HeaderText="Upload Date"
SortExpression="CreatedDate" />
<asp:BoundField DataField="CourseCode" HeaderText="Course Code" SortExpression="CourseCode" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks" SortExpression="Remarks" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm ('Are you sure to delete this record');" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
Then add sqldata source to attach to it.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LCODLConnectionString %>"
SelectCommand="LcOdlViewAcademicUploadedFiles" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Then create event of gridview ROW DELETING and paste following code in code behind model
protected void grdAcademicResourceFilesView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string rowID = Convert.ToString(grdAcademicResourceFilesView.DataKeys[e.RowIndex]["ID"]);
string uploadedFileName = grdAcademicResourceFilesView.Rows[e.RowIndex].Cells[4].Text;
if (!string.IsNullOrEmpty(uploadedFileName))
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LCODLConnectionString"].ConnectionString))
{
SqlTransaction tran = null;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
tran = con.BeginTransaction();
bool canDeleteFile = false;
try
{
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@id", rowID);
param[1] = new SqlParameter("@canDelete", SqlDbType.Bit);
param[1].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(tran, CommandType.StoredProcedure, "LcOdlDeleteUploadedAcademicFile", param);
canDeleteFile = Convert.ToBoolean(param[1].Value);
tran.Commit();
}
catch (SqlException ex)
{
tran.Rollback();
ScriptManager.RegisterStartupScript(grdAcademicResourceFilesView, grdAcademicResourceFilesView.GetType(), "DeletingError", "alert('Some thing went wrong while deleting file. Please try again later');", true);
return;
}
finally
{
con.Close();
}
//Now going To Deleet file from filesystem
string Rootfolder = Server.MapPath("~/IpSyllabusSchemas");
string FilePathWithName = string.Format("{0}\\{1}", Rootfolder, uploadedFileName);
if (canDeleteFile)
{
if (File.Exists(FilePathWithName))
{
try
{
File.Delete(FilePathWithName);
}
catch (Exception ex)
{
tran.Rollback();
ScriptManager.RegisterStartupScript(grdAcademicResourceFilesView, grdAcademicResourceFilesView.GetType(), "DeletingError", "alert('Some thing went wrong while deleting file. Please try again later');", true);
return;
}
}
}
grdAcademicResourceFilesView.DataSource = GetUploadedFiles();
grdAcademicResourceFilesView.DataBind();
}
}
}
Done :)
Comments
Post a Comment