Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > ASP.NET 4.5 General Discussion
|
ASP.NET 4.5 General Discussion For ASP.NET 4.5 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4.5 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2013, 06:46 AM
Authorized User
 
Join Date: Mar 2013
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default File Upload (and DB writing) within form view

Hi all,

I am trying to build a store and I am doing the admin end of the site and I want the user to be able to upload images to the site as well as write the filepaths into the database. I was reading through the ASP.NET 4.5 book (hoping for an usable answer) and that was done in a list view but i am trying to do it in a form view to allow for more flexible styling with CSS.

I am kinda stuck as to how to go about it in a form view.

the images paths are part of the sale table as their associated products so there's no separate table cataloging all product images.
 
Old May 5th, 2013, 08:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

As suggested earlier, please provide lots more detail: working code, your database schema, your data access technology as well as a description of what you're trying to achieve. Without all that I can't really help.

If you post code, can you please paste it in Notepad first to remove color coding and then use the forum editor's Code toolbar button to wrap your code in code tags. Otherwise, this forum messes up the code.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 5th, 2013, 08:31 AM
Authorized User
 
Join Date: Mar 2013
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default

PAGE CODE

Code:
<%@ Page Title="Empire TableTop Games" Language="C#" MasterPageFile="~/Admin.master" AutoEventWireup="true" CodeFile="AddEditItem.aspx.cs" Inherits="admin_AddEditItem" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:FormView ID="FormView1" runat="server" RenderOuterTable="False" DataKeyNames="ItemID" DataSourceID="sdsItem" DefaultMode="Insert" OnItemInserted="FormView1_ItemInserted" OnItemUpdated="FormView1_ItemUpdated">
        <EditItemTemplate>
            <h2>Update an Existing Item</h2>
            <asp:Label ID="lblCategory" runat="server" Text="Select Category:"></asp:Label><asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="sdsCategory" DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'></asp:DropDownList>
            <br />

            <asp:Label ID="lblItemID" runat="server" Text="Item Name:"/><asp:TextBox ID="ItemNameTextBox" runat="server" Text='<%# Bind("ItemName") %>' />
            <br />

           <asp:Label ID="lblPublisher" runat="server" Text="Publisher:"/><asp:DropDownList ID="ddlPublisher" runat="server" DataSourceID="sdsPublisher" DataTextField="Name" DataValueField="PublisherID" SelectedValue='<%# Bind("PublisherID") %>'></asp:DropDownList>       
            <br />

           <asp:Label ID="lblPlayers" runat="server" Text="No. of Players:"/><asp:TextBox ID="NoOfPlayersTextBox" runat="server" Text='<%# Bind("NoOfPlayers") %>' />
            <br />

            <asp:Label ID="lblPlayTime" runat="server" Text="Play Time:"/><asp:TextBox ID="PlayTimeTextBox" runat="server" Text='<%# Bind("PlayTime") %>' />
            <br />

            <asp:Label ID="lblPrice" runat="server" Text="Price:"/><asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
            <br />

            <asp:Label ID="lblThumb" runat="server" Text="Box Art (Thumbnail):"/><asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <asp:Label ID="lblCurrentThumb" runat="server" Text="Current Box Art Thumbnail:"/>
            <br />
            <asp:Image ID="imgCurrentThumb" runat="server" ImageUrl='<%#  "../" + Eval("ThumbPath") %>'/>
            <br />

            <asp:Label ID="lblFull" runat="server" Text="Box Art (Full):"/><asp:FileUpload ID="FileUpload2" runat="server" />
            <br />
            <asp:Label ID="lblCurrentFull" runat="server" Text="Current Box Art:"/>
            <br />
            <asp:Image ID="imgCurrentFull" runat="server" ImageUrl='<%#  "../" + Eval("PhotoPath") %>'/>
            <br />

            <asp:Label ID="lblDesc" runat="server" Text="Description:"/>
            <br />
            <asp:TextBox ID="DescriptionTextBox" runat="server" TextMode="MultiLine" Text='<%# Bind("Description") %>' />
            <br />

            <asp:Label ID="lblActive" runat="server" Text="Active Item?:"/>
            <asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Bind("Active") %>' />
            <br />

            <asp:Label ID="lblAvailability" runat="server" Text="Availibility Status:"/><asp:DropDownList ID="ddlSaleState" runat="server" DataSourceID="sdsAvailability" DataTextField="Name" DataValueField="SaleStateID" SelectedValue='<%# Bind("SaleStateID") %>'></asp:DropDownList>
            <br />





            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </EditItemTemplate>
        <InsertItemTemplate>

            <h2>Insert a New Item</h2>

            <asp:Label ID="lblCategory" runat="server" Text="Select Category:"></asp:Label><asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="sdsCategory" DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'></asp:DropDownList>
            <br />

           <asp:Label ID="lblName" runat="server" Text="Name:"/><asp:TextBox ID="ItemNameTextBox" runat="server" Text='<%# Bind("ItemName") %>' />
            <br />

            <asp:Label ID="lblPublisher" runat="server" Text="Publisher:"/><asp:DropDownList ID="ddlPublisher" runat="server" DataSourceID="sdsPublisher" DataTextField="Name" DataValueField="PublisherID" SelectedValue='<%# Bind("PublisherID") %>'></asp:DropDownList>       
            <br />

            <asp:Label ID="lblPlayers" runat="server" Text="No. of Players:"/><asp:TextBox ID="NoOfPlayersTextBox" runat="server" Text='<%# Bind("NoOfPlayers") %>' />
            <br />

            <asp:Label ID="lblPlayTime" runat="server" Text="Play Time:"/><asp:TextBox ID="PlayTimeTextBox" runat="server" Text='<%# Bind("PlayTime") %>' />
            <br />

            <asp:Label ID="lblPrice" runat="server" Text="Price:"/><asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
            <br />

            <asp:Label ID="lblThumb" runat="server" Text="Box Art (Thumbnail):"/><asp:FileUpload ID="FileUpload1" runat="server" />
            <br />

            <asp:Label ID="lblFull" runat="server" Text="Box Art (Full):"/><asp:FileUpload ID="FileUpload2" runat="server" />
            <br />

            <asp:Label ID="lblDesc" runat="server" Text="Description:"/>
            <br />
            <asp:TextBox ID="DescriptionTextBox" runat="server" TextMode="MultiLine" Text='<%# Bind("Description") %>' />
            <br />

            <asp:Label ID="lblActive" runat="server" Text="Active Item?:"/>
            <asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Bind("Active") %>' />
            <br />

            <asp:Label ID="lblAvailability" runat="server" Text="Availibility Status:"/><asp:DropDownList ID="ddlSaleState" runat="server" DataSourceID="sdsAvailability" DataTextField="Name" DataValueField="SaleStateID" SelectedValue='<%# Bind("SaleStateID") %>'></asp:DropDownList>
            <br />

            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
            &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </InsertItemTemplate>
        


    </asp:FormView>
    
    <asp:SqlDataSource ID="sdsAvailability" runat="server" ConnectionString="<%$ ConnectionStrings:TTEConnectionString %>" SelectCommand="SELECT [SaleStateID], [Name] FROM [ItemState]"></asp:SqlDataSource>

    <asp:SqlDataSource ID="sdsPublisher" runat="server" ConnectionString="<%$ ConnectionStrings:TTEConnectionString %>" SelectCommand="SELECT [PublisherID], [Name] FROM [Publisher] ORDER BY [Name]"></asp:SqlDataSource>

    <asp:SqlDataSource ID="sdsCategory" runat="server" ConnectionString="<%$ ConnectionStrings:TTEConnectionString %>" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Category]"></asp:SqlDataSource>

    <asp:SqlDataSource ID="sdsItem" runat="server" ConnectionString="<%$ ConnectionStrings:TTEConnectionString %>" DeleteCommand="DELETE FROM [Item] WHERE [ItemID] = @ItemID" InsertCommand="INSERT INTO [Item] ([ItemName], [PublisherID], [NoOfPlayers], [PlayTime], [Price], [ThumbPath], [PhotoPath], [Description], [Active], [CategoryID], [SaleStateID]) VALUES (@ItemName, @PublisherID, @NoOfPlayers, @PlayTime, @Price, @ThumbPath, @PhotoPath, @Description, @Active, @CategoryID, @SaleStateID)" SelectCommand="SELECT * FROM [Item] WHERE ([ItemID] = @ItemID)" UpdateCommand="UPDATE [Item] SET [ItemName] = @ItemName, [PublisherID] = @PublisherID, [NoOfPlayers] = @NoOfPlayers, [PlayTime] = @PlayTime, [Price] = @Price, [ThumbPath] = @ThumbPath, [PhotoPath] = @PhotoPath, [Description] = @Description, [Active] = @Active, [CategoryID] = @CategoryID, [SaleStateID] = @SaleStateID WHERE [ItemID] = @ItemID">
        <DeleteParameters>
            <asp:Parameter Name="ItemID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="ItemName" Type="String" />
            <asp:Parameter Name="PublisherID" Type="Int32" />
            <asp:Parameter Name="NoOfPlayers" Type="String" />
            <asp:Parameter Name="PlayTime" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="ThumbPath" Type="String" />
            <asp:Parameter Name="PhotoPath" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Active" Type="Boolean" />
            <asp:Parameter Name="CategoryID" Type="Int32" />
            <asp:Parameter Name="SaleStateID" Type="Int32" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="ItemID" QueryStringField="ID" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="ItemName" Type="String" />
            <asp:Parameter Name="PublisherID" Type="Int32" />
            <asp:Parameter Name="NoOfPlayers" Type="String" />
            <asp:Parameter Name="PlayTime" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="ThumbPath" Type="String" />
            <asp:Parameter Name="PhotoPath" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Active" Type="Boolean" />
            <asp:Parameter Name="CategoryID" Type="Int32" />
            <asp:Parameter Name="SaleStateID" Type="Int32" />
            <asp:Parameter Name="ItemID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    
</asp:Content>
SCHEMA

http://i.imgur.com/hWnLgtE.png

i am connecting through SQL Datasources to SQL Server DBs.

AIM: to upload images to a pre-set folder in the application and write the path into the database...more precisely the i want to write the file paths of 2 files into 2 fields within the Item table (want to write to the ThumbPath and PhotoPath fields).

Let me know if more is needed.

Last edited by thgh0sts; May 5th, 2013 at 08:33 AM..
 
Old May 5th, 2013, 08:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Let me know if more is needed.
Check my previous reply to see what's missing....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 5th, 2013, 08:35 AM
Authorized User
 
Join Date: Mar 2013
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default

CODE BEHIND

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EmpireTableTopModel;

public partial class admin_AddEditItem : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Get("Id") != null)
        {
            FormView1.DefaultMode = FormViewMode.Edit;
        }
    }

    private void EndEditing()
    {
        Response.Redirect("MaintainItems.aspx");
    }
    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            EndEditing();
        }
    }
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        if (e.Exception == null)
        {
            EndEditing();
        }
    }
}
Quote:
Originally Posted by Imar View Post
Check my previous reply to see what's missing....

Imar
ok, i think i have posted the right info. it's using MS SQL server and using SQL Datasources to access the information.

Last edited by thgh0sts; May 5th, 2013 at 08:37 AM..
 
Old May 5th, 2013, 01:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
ok, i think i have posted the right info. it's using MS SQL server and using SQL Datasources to access the information.
You haven't. Again, check out my previous replies. You haven't described what you're trying to accomplish and where you get stuck. All you did was post code and an EF diagram you're not even using and that as such a) doesn't describe your actual database and b) doesn't add a lot of value. What type of help are you looking for and how do you expect we can even help you based on what you've posted here?

BTW:
Quote:
but i am trying to do it in a form view to allow for more flexible styling with CSS.
A ListView will give you way more flexibility to do styling using CSS than a FormView ever can....

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 6th, 2013, 07:52 AM
Authorized User
 
Join Date: Mar 2013
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default

i am trying to find a way to implement an upload system, similar to that shown in the book, but within a formview using an sqldatasource.

below is a link to the schema in SSMS.

http://i.imgur.com/7zbrPGD.png
 
Old May 6th, 2013, 08:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

And what is the problem you're having? You have a FileUpload control in your template so why don't you hook into one of the events of the FormView and save the file?

The more details you provide about what you want to do, what you tried, what works and what doesn't work really helps. Also, replying back to me helps to get a dialog going. I asked for more details at least four times. You never supplied the details I asked for but you also didn't say you didn't understand what I asked for. This makes it hard for to help you. With a properly formulated question, this could have been solved already....

Also, I commented on the ListView versus the FormView. Any thoughts on that?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 6th, 2013, 09:20 AM
Authorized User
 
Join Date: Mar 2013
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default

OK, my level of understanding in ASP.NET isn't great so I don't understand some of the things you're asking for. I haven't even gotten to ADO.NET yet, but only got a brief understanding of EF and LINQ and in this I don't think i am using any of them for this problem.

I am just unsure about how to write the code-behind to do what was done in the book. the way it was done in the book, was that the images and albums have a many-to-1 relationship and in their own table while in my table isn't like that at all (I have 2 images for each product combined into a table called 'Item')...so basically i tried to convert code in the book into a formview and failed. One error i got was 'insert disabled for this control'.

I find formview, once you disable the outer table, much easier to handle and it's only on a single record basis and the formatting of listview to get in the way. I use the listview to generate my menus if they're being called from an SQL data source.
 
Old May 6th, 2013, 09:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
so I don't understand some of the things you're asking for.
Then please ask for further clarification. I am primarily asking you to explain what you're trying to accomplish (which you have now done for the first time) which is not related to technology at all.

Quote:
my table isn't like that at all (I have 2 images for each product combined into a table called 'Item')...
That probably makes it easier. Then again, in my book you're inserting into the Picture table which is stand alone as well (even though it's related to an album).

Quote:
so basically i tried to convert code in the book into a formview and failed. One error i got was 'insert disabled for this control'
Can you post the code you have? That will make it easier to see what you're doing.

Right now, there's a lot of code. There are at least four SqlDataSource controls, most of which are probably irrelevant for the problem at hand. Then there's lots of code for the FormView, most of which is irrelevant too.
What you typically should do to get the best answer on a forum is to get rid of all the stuff you don't need. That leaves a single SDS, a few columns (including the image columns and the primary key) and that's pretty much it. That way, it''s easy to see what you're doing, and also easy to load it up in an existing Visual Studio project and write a solution for you. Right no, there's too much junk which means it'll take way too much time to sort it out and clean it up, and thus it's unlikely you get a useful answer.

Finally, I now noticed you modified your questions and added more detail. I completely missed that as I get notifications of new messages by e-mail only.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
File Upload in form view thgh0sts BOOK: Beginning ASP.NET 4.5 : in C# and VB 4 May 5th, 2013 08:16 AM
Upload file and insert filename into access db pluribus Classic ASP Databases 0 February 28th, 2013 02:50 PM
Creating a file upload form with PHP gilbertsavier Beginning PHP 0 July 31st, 2009 06:41 AM
How to upload complex xml file to 1 db table vangogh XML 3 October 8th, 2007 04:43 AM
Q. How do I upload files to a DB and view them? richard.york PHP FAQs 0 April 3rd, 2004 07:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.