Link to home
Start Free TrialLog in
Avatar of hacresIT
hacresITFlag for United States of America

asked on

Deleting FIle & Database Record Using DataList Control

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-----
Avatar of MrAgile
MrAgile
Flag of Australia image

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


Avatar of hacresIT

ASKER

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....
I figured it out. I will close out this question and ask for refund.
ASKER CERTIFIED SOLUTION
Avatar of hacresIT
hacresIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator