locked
clubsite whitepaper errors? RRS feed

All replies

  • User-100692410 posted
    You get the Browse button by using a FileUpload server control. This is part of the control and shows up automatically.

    Bill Evjen
    Friday, July 15, 2005 1:04 PM
  • User-1391686984 posted

    Getting the browse button is not a problem, but i don't know what code to put behind this button as it does not go through this in the whitepaper for extendind the clubsite.

    All it says is,

    "These are the parameters that we will generate dynamically in the Button2_Click event (shown shortly)"

    But it is not shown after this or anywhere in the whitepaper.
    The code missing for this button should be the code to upload the document and store its details in the database.
    You cannot upload files without it

    Regards,

    Ronan. 

    Wednesday, July 27, 2005 10:05 AM
  • User1838581992 posted
    I agree.  An otherwise excellent paper is seriously marred by the exclusion of this important piece.  I was able to kludge past it with some less-than-elegant coding to get files to upload (email me for details), but I suspect Mr. Evjen has a much better solution for us.
    -Reese

    Friday, August 5, 2005 7:23 PM
  • User883927766 posted

    I found the white paper on Extending the Club site very useful in many ways as a relative novice to programming, not to site dev:

    1. I finally learned about insert parameter syntax, and how to use it in a button click event to create a simple form without using the FormView Button. Thanks!
    2. I learned how to create and consume querry strings in a hyperlink and use the in a datalist control or other control to redirect. Thanks!
    3. I learned how to create a Shared Documents section for my site!!! Thanks!

    Now all you have to do is post the page code for the add documents page, or/ and button click event for Button 2.  PLEASE!!!!!

    Sunday, August 7, 2005 8:08 AM
  • User-100692410 posted

    Sorry. I was on vacation up near the artic circle for awhile (really!). Here is the code for the Button2_Click event:

        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If FileUpload1.HasFile Then
                Try
                    FileUpload1.SaveAs(Server.MapPath("~\Files\" & FileUpload1.FileName.ToString()))
                    Label1.Text = "<b>File Successfully Uploaded!</b><br />"
                   
                    SqlDataSource2.DeleteParameters.Add("DeleteDocumentName", FileUpload1.FileName.ToString())
                    SqlDataSource2.Delete()
                                 
                    SqlDataSource2.InsertParameters.Add("DocumentName", FileUpload1.FileName.ToString())
                    SqlDataSource2.InsertParameters.Add("DateInput", DateTime.Now.ToString())
                   
                    SqlDataSource2.Insert()
                Catch ex As Exception
                    Label1.Text = "ERROR IN UPLOADING: " & ex.Message.ToString()
                Finally
                    TextBox2.Text = ""
                End Try
            End If
        End Sub

    Here is the code for the entire page:

    <%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Untitled Page" MaintainScrollPositionOnPostback="true" %>

    <script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            SqlDataSource1.Insert()
            TextBox1.Text = ""
            CheckBox1.Checked = False
    End Sub

        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If FileUpload1.HasFile Then
                Try
                    FileUpload1.SaveAs(Server.MapPath("~\Files\" & FileUpload1.FileName.ToString()))
                    Label1.Text = "<b>File Successfully Uploaded!</b><br />"
                   
                    SqlDataSource2.DeleteParameters.Add("DeleteDocumentName", FileUpload1.FileName.ToString())
                    SqlDataSource2.Delete()
                                 
                    SqlDataSource2.InsertParameters.Add("DocumentName", FileUpload1.FileName.ToString())
                    SqlDataSource2.InsertParameters.Add("DateInput", DateTime.Now.ToString())
                   
                    SqlDataSource2.Insert()
                Catch ex As Exception
                    Label1.Text = "ERROR IN UPLOADING: " & ex.Message.ToString()
                Finally
                    TextBox2.Text = ""
                End Try
            End If
        End Sub
    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <div id="body">
            <div class="fullwidth">
                <h2>File Category System</h2>
                <p>
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="SqlDataSource1" EmptyDataText="There are no categories present." Width="100%">
                        <Columns>
                            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
                                ReadOnly="True" SortExpression="CategoryID" />
                            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
                            <asp:CheckBoxField DataField="IsPublic" HeaderText="IsPublic" SortExpression="IsPublic" />
                        </Columns>
                    </asp:GridView>
                    &nbsp;<p>
                        <strong><span style="text-decoration: underline">Add a New Category</span></strong></p>
                <p>
                        Category Name:<br />
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></p>
                <p>
                    <asp:CheckBox ID="CheckBox1" runat="server" Text="Check if category is public" />&nbsp;</p>
                <p>
                    <asp:Button ID="Button1" runat="server" Text="Add Category" OnClick="Button1_Click" />
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>" DeleteCommand="DELETE FROM [DocumentCategories] WHERE [CategoryID] = @original_CategoryID" InsertCommand="INSERT INTO [DocumentCategories] ([CategoryName], [IsPublic]) VALUES (@CategoryName, @IsPublic)" SelectCommand="SELECT * FROM [DocumentCategories]" UpdateCommand="UPDATE [DocumentCategories] SET [CategoryName] = @CategoryName, [IsPublic] = @IsPublic WHERE [CategoryID] = @original_CategoryID">
                        <DeleteParameters>
                            <asp:Parameter Name="original_CategoryID" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="CategoryName" Type="String" />
                            <asp:Parameter Name="IsPublic" Type="Boolean" />
                            <asp:Parameter Name="original_CategoryID" Type="Int32" />
                        </UpdateParameters>
                        <InsertParameters>
                            <asp:ControlParameter Name="CategoryName" Type="String" ControlID="TextBox1" />
                            <asp:ControlParameter Name="IsPublic" Type="Boolean" ControlID="CheckBox1" />
                        </InsertParameters>
                    </asp:SqlDataSource>
                </p>           
            </div>
           
            <div class="fullwidth">
                <h2>File Management System</h2>
                <p>
                    <asp:GridView ID="GridView2" runat="server" AllowPaging="True" AllowSorting="True"
                        AutoGenerateColumns="False" DataKeyNames="DocumentID" DataSourceID="SqlDataSource2"
                        EmptyDataText="There are no files present." Width="100%">
                        <Columns>
                            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                            <asp:BoundField DataField="DocumentID" HeaderText="DocumentID" InsertVisible="False"
                                ReadOnly="True" SortExpression="DocumentID" />
                            <asp:BoundField DataField="DocumentName" HeaderText="DocumentName" SortExpression="DocumentName" ReadOnly="True" />
                            <asp:BoundField DataField="DateInput" HeaderText="DateInput" SortExpression="DateInput" />
                            <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
                            <asp:BoundField DataField="DocumentCategory" HeaderText="DocumentCategory" SortExpression="DocumentCategory" />
                            <asp:CheckBoxField DataField="TopDoc" HeaderText="TopDoc" SortExpression="TopDoc" />
                        </Columns>
                    </asp:GridView>
                    &nbsp;</p>
                <p>
                    <strong><span style="text-decoration: underline">Add a New File</span></strong></p>
                <p>
                    Select Category:<br />
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                        DataTextField="CategoryName" DataValueField="CategoryID">
                    </asp:DropDownList></p>
                <p>
                    File Comments:<br />
                    <asp:TextBox ID="TextBox2" runat="server" Width="350px"></asp:TextBox></p>
                <p>
                    Show up on Top-Document List?
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                        <asp:ListItem Value="true">Yes</asp:ListItem>
                        <asp:ListItem Selected="True" Value="false">No</asp:ListItem>
                    </asp:RadioButtonList></p>
                <p>
                    File to Upload:<br />
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="350px" /></p>
                <p>
                    <asp:Button ID="Button2" runat="server" Text="Upload File" OnClick="Button2_Click" />&nbsp;</p>
                <p>
                    <asp:Label ID="Label1" runat="server"></asp:Label><br />
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                        DeleteCommand="DELETE FROM [Documents] WHERE [DocumentID] = @original_DocumentID"
                        InsertCommand="INSERT INTO [Documents] ([DocumentName], [DateInput], [Comments], [DocumentCategory], [TopDoc]) VALUES (@DocumentName, @DateInput, @Comments, @DocumentCategory, @TopDoc)"
                        SelectCommand="SELECT * FROM [Documents]" UpdateCommand="UPDATE [Documents] SET [DocumentName] = @DocumentName, [DateInput] = @DateInput, [Comments] = @Comments, [DocumentCategory] = @DocumentCategory, [TopDoc] = @TopDoc WHERE [DocumentID] = @original_DocumentID">
                        <DeleteParameters>
                            <asp:Parameter Name="original_DocumentID" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="DocumentName" Type="String" />
                            <asp:Parameter Name="DateInput" Type="DateTime" />
                            <asp:Parameter Name="Comments" Type="String" />
                            <asp:Parameter Name="DocumentCategory" Type="Int32" />
                            <asp:Parameter Name="TopDoc" Type="Boolean" />
                            <asp:Parameter Name="original_DocumentID" Type="Int32" />
                        </UpdateParameters>
                        <InsertParameters>
                            <asp:ControlParameter Name="Comments" Type="String" ControlID="TextBox2" />
                            <asp:ControlParameter Name="DocumentCategory" Type="Int32" ControlID="DropDownList1" PropertyName="SelectedValue" />
                            <asp:ControlParameter Name="TopDoc" Type="Boolean" ControlID="RadioButtonList1" PropertyName="SelectedValue" />
                        </InsertParameters>
                    </asp:SqlDataSource>
                    &nbsp;</p>
            </div> 
                
        </div>
    </asp:Content>

    HTH, Bill Evjen

    Saturday, August 13, 2005 8:27 AM
  • User883927766 posted
    Thank you so much for this code...It will make all the difference. I have tried to create the page with modifications for my program, but I have an error on the GridView2 Document Name Hyperlink. Can you show or post the code for that please....Sorry.
    You forgot to make GridView2 - DocumentName a Templated field and show the hyperlink to the file and how to retrieve it...

    Also - Is it possible to store documents outside the web on another physical drive or remote location for security, and what settings on the folder (Files) would we need to set to only allow certain types of documents (.doc, .ppt, .xls and so on)?

    Server Error "Character Not Recognized" occurs when I try to implement the strategy in the Extending Club Site Article...HELP!!!!!
    I already tried to remove the @ symbol?
    <
    ItemTemplate>

    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# <%# Eval("DocumentName", @"~\Files\{0}") %> %>'

    Text='<%# Eval("DocumentName") %>'></asp:HyperLink>

    </ItemTemplate>

    LATER That Day....
    I found two error in the above code:
    1. There are two <%# and %> signs
    2. and you do not need the @ sign, which caused the error..

    IT WORKS NOW

    Saturday, August 13, 2005 9:52 AM
  • User1356412166 posted

    I was having trouble with this code and found by adding the TypeCode to the DateTime parameter the code worked fine If anyone had sme prob here is what i did.

    SqlDataSource2.InsertParameters.Add("DateInput", TypeCode.DateTime, DateTime.Now.ToString())

    Saturday, January 21, 2006 8:17 PM
  • User-214118200 posted

    Bill, thanks for the template and tutorials.

    Testing the code I had a couple problems.

    1) Edits and deletes were failing.  The "originalDocumentID" name in the Commands and Parameters appears to be the problem, so I changed it to "DocumentID", making it consistent with the working code for SQLDataSource1.

    2) Existing UpdateCommand blanks the DocumentName when editing, because DocumentName is a read-only field

    The rewritten SQLDataSource2 section (hopefully the indents are not messed up, last time i posted, the indents in this message designer made a mess):

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
       DeleteCommand="DELETE FROM [Documents] WHERE [DocumentID] = @DocumentID"
       InsertCommand="INSERT INTO [Documents] ([DocumentName], [DateInput], [Comments], [DocumentCategory], [TopDoc]) VALUES (@DocumentName, @DateInput, @Comments, @DocumentCategory, @TopDoc)"
       SelectCommand="SELECT * FROM [Documents]"
       UpdateCommand="UPDATE [Documents] SET [DateInput] = @DateInput, [Comments] = @Comments, [DocumentCategory] = @DocumentCategory, [TopDoc] = @TopDoc WHERE [DocumentID] = @DocumentID">
       <DeleteParameters>
           <asp:Parameter Name="DocumentID" Type="Int32" />
       </DeleteParameters>
       <UpdateParameters>
           <asp:Parameter Name="DateInput" Type="DateTime" />
           <asp:Parameter Name="Comments" Type="String" />
           <asp:Parameter Name="DocumentCategory" Type="Int32" />
           <asp:Parameter Name="TopDoc" Type="Boolean" />
           <asp:Parameter Name="DocumentID" Type="Int32" />
       </UpdateParameters>
       <InsertParameters>
           <asp:ControlParameter Name="Comments" Type="String" ControlID="TextBox2" />
           <asp:ControlParameter Name="DocumentCategory" Type="Int32" ControlID="DropDownList1" PropertyName="SelectedValue" />
           <asp:ControlParameter Name="TopDoc" Type="Boolean" ControlID="RadioButtonList1" PropertyName="SelectedValue" />
           </InsertParameters>
    </asp:SqlDataSource>

     

    Monday, March 6, 2006 6:05 PM
  • User-1442657951 posted

    I have always used C and have been using the C# version of the club template.

    Does anyone have code for the button in C#? It is only given in VB above

    Dave

    Tuesday, March 14, 2006 5:53 PM
  • User-141606038 posted
    Here the C# versione of the code

    bye
    Riccc


       protected void Button2_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile) {
               
                try {
                    FileUpload1.SaveAs(Server.MapPath("~\\Files\\" + FileUpload1.FileName.ToString()));
                    Label1.Text = "<b>File Successfully Uploaded!</b><br />";
                   
                    SqlDataSource2.DeleteParameters.Add("DeleteDocumentName", FileUpload1.FileName.ToString());
                    SqlDataSource2.Delete();
                                 
                    SqlDataSource2.InsertParameters.Add("DocumentName", FileUpload1.FileName.ToString());
                    SqlDataSource2.InsertParameters.Add("DateInput", TypeCode.DateTime, DateTime.Now.ToString());
                   
                    SqlDataSource2.Insert();
                }
                catch (Exception ex) {
                    Label1.Text = "ERROR IN UPLOADING: " + ex.Message.ToString();
                }
                finally {
                    TextBox2.Text = "";
                }
            }
        }
    Thursday, April 6, 2006 6:55 AM
  • User1946081430 posted

    I had this file sharing up and running - but my host changed to "medium trust" and this no longer works. I get this error:

    ERROR IN UPLOADING: Access to the path 'c:\domains\**domainname**\wwwroot\Files\Redneckwindchime_116.jpg' is denied.

    Is there a workaround or can this not be used under medium trust?

    Thanks

     

    Wednesday, July 5, 2006 8:09 PM
  • User706866991 posted

    I get the following error when trying to upload

     

    ERROR IN UPLOADING: Cannot insert the value NULL into column 'DocumentID', table 'C:\DOCUMENTS AND SETTINGS\BOB\MY DOCUMENTS\VISUAL STUDIO 2005\WEBSITES\PRCCDEVSITE\APP_DATA\CLUB.MDF.dbo.Documents'; column does not allow nulls. INSERT fails. The statement has been terminated.

    Although I understand the error, I can not seem to find the code that increments DocumentID and inserts it

    Any suggestions?

     

    Thanks

     

    Bob

    Wednesday, July 12, 2006 10:16 AM
  • User654902800 posted
    IIRC the documentid column is an Identity column, open up the table definition, and look in the list of properties for that column, it should say Identity - yes, initial value 1, increments by 1.
    Wednesday, July 12, 2006 1:10 PM
  • User1524337749 posted
    Scott asked, "Is it possible to store documents outside the web on another physical drive or remote location for security...?"  This is just the issue I am having and I can't figure out how to code the navigateurl to handle it in the Documents.aspx page so that file will open when the user clicks on the hyperlink.  My web provider won't let the aspnet account have write/delete access to a subfolder inside of my web application.  They only give us those rights to a completely seperate folder outside my application.  I have the path to that folder and am trying to build the url using that as follows:

                               <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Bind("DocumentName","E:\webs\S123\ourweb.org\dataroot\") %>'
                                    Text='<%# Eval("DocumentName") %>'></asp:HyperLink>

    This will build but when you click on the hyperlink for the file nothing happens.  Can someone please tell me what I'm doing wrong?  Thanks!

    Fiona
     

    Wednesday, July 19, 2006 12:13 PM
  • User1524337749 posted

    I finally found one way to do this at least partially, in case this helps anyone else.  I changed it from a hyperlink to a Linkbutton so I could then progarm the onrowcommand event of the datagridview.  From onrowcommand I could then either let the user download the file or stream it to the page.  So far this only works for text files.  PDF's crash it.  Still working on that part....:

    <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument ='<%# Eval("DocumentName")%>' CommandName ="Navigate" Text='<%# Eval("DocumentName") %>'></asp:LinkButton >

    Then in the datagridview that this is part of you catch the onrowcommand event:

    <asp:GridView  onrowcommand="GridView_RowCommand"

     

    And here is what I have for that event .....

     

    Sub GridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    Dim filename As String = e.CommandArgument.ToString()

    Dim PathAndFile As String = System.Configuration.ConfigurationManager.AppSetting("DocumentPathVCHCA")

    PathAndFile = PathAndFile & filename

    Context.Response.ClearContent()

    Context.Response.ClearHeaders()

    Context.Response.AddHeader("Content-Type", "application/pdf") 'new line

    'Line below makes it prompt to download the file

    'HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & filename)

    'Line below makes it stream to browser page:

    Context.Response.AddHeader("content-Disposition", "inline;filename=testpdf")

    Response.WriteFile(pathandfile)

    Context.Response.ContentType = "application/pdf"

    Context.Response.AddHeader("content-Disposition", "filename=test")

    End Sub

    Thursday, July 20, 2006 11:27 PM
  • User1524337749 posted

    I got the rowcommand event to work so that it correctly opens or downloads either txt or pdf files.  Here's the code in case it helps someone else....

    Sub GridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    Dim filename As String = e.CommandArgument.ToString()

    Dim PathAndFile As String = System.Configuration.ConfigurationManager.AppSettings("DocumentPathVCHCA")

    PathAndFile = PathAndFile & filename

    If System.IO.File.Exists(PathAndFile) Then

    Response.Clear()

    If Right(filename, 3) = "pdf" Then

    Response.ContentType = "application/pdf"

    Else

    Response.ContentType = "text/plain"

    End If

    Response.AddHeader("Content-Disposition", "attachment; filename=""" & filename & """")

    Response.Flush()

    Response.WriteFile(PathAndFile)

    Context.Response.Flush()

    Context.Response.Close()

    Context.Response.End()

    End If

    End Sub

    Friday, July 21, 2006 10:07 PM
  • User706866991 posted

    Bill,

    I am having some layout issues with the docs page and am unable to figure it out (I am pretty new to this).  Would you have the code available for the docs page as well?

     

    Thanks!

    Bob

     

    Friday, September 8, 2006 12:49 PM
  • User523970105 posted

    Bill,

    I am having some layout issues with the docs page and am unable to figure it out (I am pretty new to this).  Would you have the code available for the docs page as well?

     

    Thanks!

    Bob

    Bob,

     I have the complete package working on GoDaddy - modified to store the files in a folder instead of the database since the size of my MSSQL database is severely limited on GoDaddy.  I can email it to you or post it if you are interested.

     

    Friday, September 8, 2006 11:27 PM
  • User523970105 posted

    I forgot to mention that what I have stores the files in a sub-folder within the club site - not in a completely separate folder as has been discussed earlier in this thread.

    Friday, September 8, 2006 11:30 PM
  • User706866991 posted

    That would be cool.  I am finally back on this and can't get the docs page to display properly.  the Admin page looks OK but if you wouldn't mind sending them both with the code so I can compare and see where I went wrong that would be great.

     

    Bob

     

    Wednesday, October 18, 2006 10:14 PM
  • User523970105 posted

    That would be cool.  I am finally back on this and can't get the docs page to display properly.  the Admin page looks OK but if you wouldn't mind sending them both with the code so I can compare and see where I went wrong that would be great.

     

    Bob

     

    Here you go.  Note that I changed the parts that are visible to web visitors from "Documents" to "Files", since it can store more than just documents.

    Instructions:

    1. Create a "files" folder off of the web root
    2. Feed files-add.sql into the query analyzer
    3. Add files.aspx and files_admin.aspx to your project.

    files-add.sql:

    CREATE TABLE [DocumentCategories] (
    	[CategoryID] [int] IDENTITY (1, 1) NOT NULL ,
    	[CategoryName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    	[IsPublic] [bit] NULL
    ) ON [PRIMARY]
    GO
    
    CREATE TABLE [Documents] (
    	[DocumentID] [int] IDENTITY (1, 1) NOT NULL ,
    	[DocumentName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    	[DateInput] [datetime] NULL ,
    	[Comments] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    	[DocumentCategory] [int] NULL ,
    	[TopDoc] [bit] NULL
    ) ON [PRIMARY]
    GO
    
    ALTER TABLE [DocumentCategories] ADD 
    	CONSTRAINT [PK_DocumentCategories] PRIMARY KEY  CLUSTERED 
    	(
    		[CategoryID]
    	)  ON [PRIMARY] 
    GO
    
    ALTER TABLE [Documents] ADD 
    	CONSTRAINT [PK_Documents] PRIMARY KEY  CLUSTERED 
    	(
    		[DocumentID]
    	)  ON [PRIMARY] 
    GO
    
      

    files.aspx

     

    <%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Files" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <div id="body">
            <div class="fullwidth">
                <h2>File Categories</h2>
                <p>Click on a category to see a list of available files.</p>
                <p>
                    <asp:DataList ID="DataList1" runat="server" DataKeyField="CategoryID" DataSourceID="SqlDataSource1">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("CategoryID", @"Files.aspx?categoryid={0}") %>'
                                Text='<%# Eval("CategoryName") %>'></asp:HyperLink>
                        </ItemTemplate>
                    </asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                        SelectCommand="SELECT * FROM [DocumentCategories] WHERE ([IsPublic] = @IsPublic) ORDER BY [CategoryName]">
                        <SelectParameters>
                            <asp:Parameter DefaultValue="True" Name="IsPublic" Type="Boolean" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </p>
            </div>
            
            <div class="fullwidth">
                <h2>Files</h2>
                <p>
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                        AutoGenerateColumns="False" DataKeyNames="DocumentID" Width="100%" DataSourceID="SqlDataSource2">
                        <Columns>
                            <asp:TemplateField HeaderText="Document Name" SortExpression="DocumentName">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DocumentName") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("DocumentName", @"~\Files\{0}") %>'
                                        Text='<%# Eval("DocumentName") %>' Target="_blank"></asp:HyperLink>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="DateInput" HeaderText="Date Input" SortExpression="DateInput" />
                            <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
                        </Columns>
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                        SelectCommand="SELECT * FROM [Documents] WHERE ([DocumentCategory] = @DocumentCategory) ORDER BY [DateInput] DESC">
                        <SelectParameters>
                            <asp:QueryStringParameter Name="DocumentCategory" QueryStringField="categoryid" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </p>
            </div>  
        </div>
    </asp:Content>
     

    files_admin.aspx

    <%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Files Management" MaintainScrollPositionOnPostback="true" %>
    <%@ Import Namespace="System.IO" %>
    
    <script runat="server">
    
        Protected Sub GridView2_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
            'Since documents are not stored in the database, they must be deleted from
            'the filesystem when the user clicks the delete button in the gridview
            File.Delete(Server.MapPath("~\files\") & e.Values(0).ToString())
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            SqlDataSource1.Insert()
            TextBox1.Text = ""
            CheckBox1.Checked = False
        End Sub
    
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If FileUpload1.HasFile Then
                Try
                    FileUpload1.SaveAs(Server.MapPath("~\files\" & FileUpload1.FileName.ToString()))
                    Label1.Text = "<b>File Successfully Uploaded!</b><br />"
                    
                    SqlDataSource2.DeleteParameters.Add("DeleteDocumentName", FileUpload1.FileName.ToString())
                    SqlDataSource2.Delete()
                                  
                    SqlDataSource2.InsertParameters.Add("DocumentName", FileUpload1.FileName.ToString())
                    SqlDataSource2.InsertParameters.Add("DateInput", DateTime.Now.ToString())
                    
                    SqlDataSource2.Insert()
                Catch ex As Exception
                    Label1.Text = "ERROR IN UPLOADING: " & ex.Message.ToString()
                Finally
                    TextBox2.Text = ""
                End Try
            End If
        End Sub
    </script>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <div id="body">
            <div class="fullwidth">
                <h2>File Category System</h2>
                <p>
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                    AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="SqlDataSource1"
                    EmptyDataText="There are no categories present." Width="100%">
                        <Columns>
                            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
                                ReadOnly="True" SortExpression="CategoryID" />
                            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
                            <asp:CheckBoxField DataField="IsPublic" HeaderText="IsPublic" SortExpression="IsPublic" />
                        </Columns>
                    </asp:GridView>
                    <p>
                        <strong><span style="text-decoration: underline">Add a New Category</span></strong></p>
                <p>
                        Category Name:<br />
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></p>
                <p>
                    <asp:CheckBox ID="CheckBox1" runat="server" Text="Check if category is public" /></p>
                <p>
                    <asp:Button ID="Button1" runat="server" Text="Add Category" OnClick="Button1_Click" />
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                    DeleteCommand="DELETE FROM [DocumentCategories] WHERE [CategoryID] = @CategoryID"
                    InsertCommand="INSERT INTO [DocumentCategories] ([CategoryName], [IsPublic]) VALUES (@CategoryName, @IsPublic)"
                    SelectCommand="SELECT * FROM [DocumentCategories]"
                    UpdateCommand="UPDATE [DocumentCategories] SET [CategoryName] = @CategoryName, [IsPublic] = @IsPublic WHERE [CategoryID] = @CategoryID">
                        <DeleteParameters>
                            <asp:Parameter Name="CategoryID" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="CategoryName" Type="String" />
                            <asp:Parameter Name="IsPublic" Type="Boolean" />
                            <asp:Parameter Name="CategoryID" Type="Int32" />
                        </UpdateParameters>
                        <InsertParameters>
                            <asp:ControlParameter Name="CategoryName" Type="String" ControlID="TextBox1" />
                            <asp:ControlParameter Name="IsPublic" Type="Boolean" ControlID="CheckBox1" />
                        </InsertParameters>
                    </asp:SqlDataSource>
                </p>            
            </div>
            
            <div class="fullwidth">
                <h2>File Management System</h2>
                <p>
                    <asp:GridView ID="GridView2" runat="server" AllowPaging="True" AllowSorting="True"
                        AutoGenerateColumns="False" DataKeyNames="DocumentID" DataSourceID="SqlDataSource2"
                        OnRowDeleting="GridView2_RowDeleting" EmptyDataText="There are no files present." Width="100%">
                        <Columns>
                            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                            <asp:BoundField DataField="DocumentID" HeaderText="DocumentID" InsertVisible="False"
                                ReadOnly="True" SortExpression="DocumentID" />
                            <asp:BoundField DataField="DocumentName" HeaderText="DocumentName" SortExpression="DocumentName" ReadOnly="True" />
                            <asp:BoundField DataField="DateInput" HeaderText="DateInput" SortExpression="DateInput" />
                            <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
                            <asp:BoundField DataField="DocumentCategory" HeaderText="DocumentCategory" SortExpression="DocumentCategory" />
                            <asp:CheckBoxField DataField="TopDoc" HeaderText="TopDoc" SortExpression="TopDoc" />
                        </Columns>
                    </asp:GridView>
                     </p>
                <p>
                    <strong><span style="text-decoration: underline">Add a New File</span></strong></p>
                <p>
                    Select Category:<br />
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                        DataTextField="CategoryName" DataValueField="CategoryID">
                    </asp:DropDownList></p>
                <p>
                    File Comments:<br />
                    <asp:TextBox ID="TextBox2" runat="server" Width="350px"></asp:TextBox></p>
                <p>
                    Show up on Top-Document List?
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                        <asp:ListItem Value="true">Yes</asp:ListItem>
                        <asp:ListItem Selected="True" Value="false">No</asp:ListItem>
                    </asp:RadioButtonList></p>
                <p>
                    File to Upload:<br />
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="350px" /></p>
                <p>
                    <asp:Button ID="Button2" runat="server" Text="Upload File" OnClick="Button2_Click" /> </p>
                <p>
                    <asp:Label ID="Label1" runat="server"></asp:Label><br />
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                        DeleteCommand="DELETE FROM [Documents] WHERE [DocumentID] = @DocumentID"
                        InsertCommand="INSERT INTO [Documents] ([DocumentName], [DateInput], [Comments], [DocumentCategory], [TopDoc]) VALUES (@DocumentName, @DateInput, @Comments, @DocumentCategory, @TopDoc)"
                        SelectCommand="SELECT * FROM [Documents]"
                        UpdateCommand="UPDATE [Documents] SET [DocumentName] = @DocumentName, [DateInput] = @DateInput, [Comments] = @Comments, [DocumentCategory] = @DocumentCategory, [TopDoc] = @TopDoc WHERE [DocumentID] = @DocumentID">
                        <DeleteParameters>
                            <asp:Parameter Name="DocumentID" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="DocumentName" Type="String" />
                            <asp:Parameter Name="DateInput" Type="DateTime" />
                            <asp:Parameter Name="Comments" Type="String" />
                            <asp:Parameter Name="DocumentCategory" Type="Int32" />
                            <asp:Parameter Name="TopDoc" Type="Boolean" />
                            <asp:Parameter Name="DocumentID" Type="Int32" />
                        </UpdateParameters>
                        <InsertParameters>
                            <asp:ControlParameter Name="Comments" Type="String" ControlID="TextBox2" />
                            <asp:ControlParameter Name="DocumentCategory" Type="Int32" ControlID="DropDownList1" PropertyName="SelectedValue" />
                            <asp:ControlParameter Name="TopDoc" Type="Boolean" ControlID="RadioButtonList1" PropertyName="SelectedValue" />
                        </InsertParameters>
                    </asp:SqlDataSource>
                     </p>
            </div>  
                 
        </div>
    </asp:Content>
     

     

    Thursday, October 19, 2006 11:16 AM
  • User706866991 posted

    Super!  Thanks!  Mine was just a formatting error and it didn't display the File Category links correctly.  Not sure waht ws causing it.  I'll compare yours to mine and see if I can figure it out.

     

    Bob

     

    Thursday, October 19, 2006 7:11 PM
  • User-1716368789 posted

    Using the whitepaper and then Bill's code I have the fileupload system working but here is my question.

     When you remove a file via the datagrid it removes the database entry but does not delete the file itself.

    How do I get the program to also remove the file from the server and not just the table?

    Thanks in advance

    Tuesday, May 1, 2007 5:27 AM
  • User-1716368789 posted

    Forget that last thread after re-reading the post I found the answer.

    acutally have the site up and running now www.shattereddestinies.net

    Wednesday, May 9, 2007 4:38 AM