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
Solved

Deleting FIle & Database Record Using DataList Control

Posted on 2007-12-05
6
440 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
imap mails 1 25
Export PDF Form fields to Access  or Excel  in Tab order 16 80
IEnumerable<T> to a List<T> 8 38
Please explain purpose of GZIP 4 35
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

837 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