Solved

Deleting FIle & Database Record Using DataList Control

Posted on 2007-12-05
6
434 Views
Last Modified: 2013-12-25
Heyas;

Ok, This is a very complex problem I've been playing with for a few days now, and I am hoping that someone can help me out. I am using VB.NET and ASP.NET 2.0. For my IDE I am using VS2005

------problem---------
My first problem is 'finding' the button controls in the DataList control so I can assign the following actions, then when the user clicks on the 'delete' button in a DataList control, the following needs to take place:

1. Delete three files (a PDF, PPS, and MP3) from the server directory USING the filename currently stored in the database.

2. One the files are deleted, I need the database to delete that particular record from the database

Deleting the database record is easy, but I have no idea how to delete a file based on a field in the database.
--------------------------------------
Ok, so this is what I have so far; two files

---------ascx file------------------
<asp:DataList ID="DataList1" runat="server" DataKeyField="FileID" DataSourceID="WebinarAdmin">
    <ItemTemplate>
        <div style="text-align: center">
        <table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
            <tr>
                <td style="width: 100px"></td>
                <td style="width: 100px">
                    <asp:Label ID="Label3" runat="server" Text="File ID"></asp:Label></td>
                    <td style="width: 100px">
                    <asp:Label ID="Label4" runat="server" Text="Month"></asp:Label></td>
                    <td align="center" style="width: 100px">
                    <asp:Image ID="Image1" runat="server" ImageUrl="~/images/icons/pdf16.gif" /></td>
                    <td align="center" style="width: 100px">
                    <asp:Image ID="Image2" runat="server" ImageUrl="~/images/icons/ppt16.gif" /></td>
                    <td align="center" style="width: 100px">
                    <asp:Image ID="Image3" runat="server" ImageUrl="~/images/icons/wav.gif" /></td>
            </tr>
       
            <tr>
                <td style="width: 100px">
                    <asp:Button ID="btnDelete" runat="server" OnClick="Button1_Click" Text="Delete" />
                    <asp:Button ID="btnEdit" runat="server" Text="Edit" /></td>
                <td style="width: 100px">
                    <asp:Label ID="FileIDLabel" runat="server" Text='<%# Eval("FileID") %>'>
                    </asp:Label></td>
                <td style="width: 100px">
                    <asp:Label ID="MonthLabel" runat="server" Text='<%# Eval("Month") %>'>
                    </asp:Label></td>
                <td style="width: 100px">
                    <asp:Label ID="PDFLabel" runat="server" Text='<%# Eval("PDF") %>'></asp:Label></td>
                <td style="width: 100px">
                    <asp:Label ID="PPSLabel" runat="server" Text='<%# Eval("PPS") %>'></asp:Label></td>
                <td style="width: 100px">
                    <asp:Label ID="AudioLabel" runat="server" Text='<%# Eval("Audio") %>'>
                    </asp:Label></td>
            </tr>
        </table>
        </div>
    </ItemTemplate>
</asp:DataList>

<asp:SqlDataSource ID="WebinarAdmin" runat="server" ConnectionString="<%$ ConnectionStrings:Hacres %>"
                   
SelectCommand="SELECT * FROM [HMWebinar] WHERE (([PostDate] > @PostDate) AND ([PostDate] < @PostDate2))"

DeleteCommand="DELETE FROM [HMWebinar] WHERE [FileID] = @FileID" InsertCommand="INSERT INTO [HMWebinar] ([PostDate], [Month], [PDF], [PPS], [Audio]) VALUES (@PostDate, @Month, @PDF, @PPS, @Audio)"

UpdateCommand="UPDATE [HMWebinar] SET [PostDate] = @PostDate, [Month] = @Month, [PDF] = @PDF, [PPS] = @PPS, [Audio] = @Audio WHERE [FileID] = @FileID">

    <SelectParameters>
        <asp:ControlParameter ControlID="txtStartDate" Name="PostDate" PropertyName="Text"
            Type="DateTime" />
        <asp:ControlParameter ControlID="txtEndDate" Name="PostDate2" PropertyName="Text"
            Type="DateTime" />
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter Name="FileID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="PostDate" Type="DateTime" />
        <asp:Parameter Name="Month" Type="String" />
        <asp:Parameter Name="PDF" Type="String" />
        <asp:Parameter Name="PPS" Type="String" />
        <asp:Parameter Name="Audio" Type="String" />
        <asp:Parameter Name="FileID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="PostDate" Type="DateTime" />
        <asp:Parameter Name="Month" Type="String" />
        <asp:Parameter Name="PDF" Type="String" />
        <asp:Parameter Name="PPS" Type="String" />
        <asp:Parameter Name="Audio" Type="String" />
    </InsertParameters>
</asp:SqlDataSource>
-------------- End ASP.NET file -------------------

-------------- VB Code Behind -------------------
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim PDF As File = Server.MapPath("~/health/webinars/") & WebinarAdmin 'don't know what goes here
    Dim PPS As File = Server.MapPath("~/health/webinars/") & PPSField 'don't know what goes here
    Dim Audio As File = Server.MapPath("~/health/webinars/") & AudioField 'don't know what goes here

    If PDF.Exists Then
        File.Delete(PDF)
        File.Delete(PPS)
        File.Delete(Audio)
    End If
   
    ' SQL Command
    WebinarAdmin.Delete()
    End Sub

----- End VB Code-----
0
Comment
Question by:hacresIT
  • 3
6 Comments
 
LVL 8

Expert Comment

by:MrAgile
Comment Utility
HI There,

The first part of the question 'finding the control'. You need to use the itemcommand event if you're going to be clicking a button inside a datalist control.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemcommand.aspx

2. To modify files:

http://msdn2.microsoft.com/en-us/library/system.io.file.aspx

If you're using visual studio I would recommend using the f1 key and going through the help. This will give you a better understanding of .net concepts and syntax. If you don't use vs then msdn is a great resource.

Let me know if I can help you further. Sean


0
 
LVL 1

Author Comment

by:hacresIT
Comment Utility
alright. I'm still a bit confused. I thought to change tactics. I switched to a Gridview control. It deletes the file from the database, but I'm still not able to get it to delete the file from the folder based upon the value in a cell from the selected row. This is what I have so far;(vb.net)

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

        Dim PDF As String

        PDF = GridView1.SelectedRow().Cells(3).Text 'Selecting item in 3th column

        File.Delete(Server.MapPath("~/health/webinars/") & PDF)

    End Sub

I am getting a "Object reference not set to an instance of an object. " error....
0
 
LVL 1

Author Comment

by:hacresIT
Comment Utility
I figured it out. I will close out this question and ask for refund.
0
 
LVL 1

Accepted Solution

by:
hacresIT earned 0 total points
Comment Utility
Here was the answer to my question;

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

        Dim path As String
        Dim PDF As String
        Dim PPS As String
        Dim Audio As String

        path = Server.MapPath("~/health/webinars/")

        PDF = GridView1.Rows(e.RowIndex).Cells(4).Text
        PPS = GridView1.Rows(e.RowIndex).Cells(5).Text
        Audio = GridView1.Rows(e.RowIndex).Cells(6).Text

        File.Delete(path & PDF)
        File.Delete(path & PPS)
        File.Delete(path & Audio)

    End Sub
0
 
LVL 1

Expert Comment

by:Vee_Mod
Comment Utility
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now