Solved

Deleting FIle & Database Record Using DataList Control

Posted on 2007-12-05
6
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 8

Expert Comment

by:MrAgile
ID: 20414606
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
ID: 20430168
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
ID: 20599338
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
ID: 20599589
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
ID: 20640724
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

617 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