Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Beginning ASP.NET 4.5 : in C# and VB
|
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB 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 November 16th, 2014, 07:58 PM
Registered User
 
Join Date: Oct 2014
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Using Image Control with ListView and SQLDataSource

I have been following the text book, (It's a great piece of work).

But my problem is my images do not display in the browser. My project requires one database table with the following 4 columns: Id, Name, Description & ImageUrl.

The images upload to the photo folder as instructed in the book. Everything works except I cant work out how to code the statement that updates the class instance to show the image on the page.


Code:
<%@ Page Title="Trainer Profiles" Language="C#"  Debug="true"  MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="TrainerProfiles.aspx.cs" Inherits="_TrainerProfiles" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    
    <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="Id" InsertItemPosition="none" OnItemCreated="ListView1_ItemCreated">
                                 
          <InsertItemTemplate>
            <span style="">Name:
            <asp:TextBox ID="NameTextBox" Width="200" runat="server" Text='<%# Bind("Name") %>' />
            <br />
            Background:
            <asp:TextBox ID="BackgroundTextBox" TextMode="MultiLine" Width ="200" Height="200" runat="server" Text='<%# Bind("Background") %>' />
            <br />
                <asp:FileUpload ID="FileUpload2" runat="server" />
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" CausesValidation="False" />
            <br />
            <br />
            </span>
        </InsertItemTemplate>
        
        <ItemTemplate>
            <span style="">
             Name:
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
            <br />
            Background:
            <asp:Label ID="BackgroundLabel" runat="server" Text='<%# Eval("Background") %>'   />
            <br />
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' ImageAlign="Right" Width="180px" AlternateText='<%# Eval("Name") %>'/>
            <br />
           <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="False" />
            <br />
            <br />
            </span>
        </ItemTemplate>
       
        <LayoutTemplate>
            <div style="" id="itemPlaceholderContainer" runat="server">
                <span runat="server" id="itemPlaceholder" />
            </div>
            <div style="">
            </div>

         <asp:DataPager ID="ProfilePager" runat="server" PagedControlID="ListView1" PageSize="4">
        <Fields>
            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
            <asp:NumericPagerField />
            <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
        </Fields>
    </asp:DataPager>

        </LayoutTemplate>
        
        
         </asp:ListView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:McCabeYourHealthPTConnectionString1 %>" SelectCommand="SELECT * FROM [TrainerProfile]" DeleteCommand="DELETE FROM [TrainerProfile] WHERE [Id] = @original_Id" InsertCommand="INSERT INTO [TrainerProfile] ([Name], [Background], [ImageUrl]) VALUES (@Name, @Background, @ImageUrl)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [TrainerProfile] SET [Name] = @Name, [Background] = @Background, [ImageUrl] = @ImageUrl WHERE [Id] = @original_Id" OnInserting="SqlDataSource1_Inserting" OnSelecting="SqlDataSource1_Selecting">
        <DeleteParameters>
            <asp:Parameter Name="original_Id" Type="Int16" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Background" Type="String" />
            <asp:Parameter Name="ImageUrl" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Background" Type="String" />
            <asp:Parameter Name="ImageUrl" Type="String" />
            <asp:Parameter Name="original_Id" Type="Int16" />
        </UpdateParameters>
    </asp:SqlDataSource>
    
    </asp:Content>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;



public partial class _TrainerProfiles : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        string virtualFolder = "~/Management/ProfilePics/";
        string physicalFolder = Server.MapPath(virtualFolder);
        string fileName = Guid.NewGuid().ToString();
        string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
    }   

       
    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
        {
            CustomValidator cusValImage = (CustomValidator)ListView1.InsertItem.FindControl("cusValImage");
            cusValImage.IsValid = false;
            e.Cancel = true;
        }
    }


    protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListViewItemType.DataItem:
                Button deleteButton = (Button)e.Item.FindControl("DeleteButton");
                deleteButton.Visible = Roles.IsUserInRole("Managers");
                break;
        }
          
        if (Page.User.IsInRole("Managers"))
        {

            ListView1.InsertItemPosition = InsertItemPosition.LastItem;

        }
    }

    protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
}
 
Old November 17th, 2014, 08:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It looks like you're not saving the ImageUrl to the database. When you look in the database, what do you see for the ImageUrl column?

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!
The Following User Says Thank You to Imar For This Useful Post:
 
Old November 17th, 2014, 02:22 PM
Registered User
 
Join Date: Oct 2014
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hello Imar,
When I look at the database ImageUrl column is shows null for the record.

Marie
 
Old November 19th, 2014, 06:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, that's what I thought. You're not assigning the Image URL to a column in the database. You need to handle the Inserting event and then assign something like e.Values["ImageUrl"] the URL of the image. Take a look at AddEditReview.aspx in the Management folder for some inspiration.

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
CH14 Customizing Templates of the ListView Control (image issue) agiusthomas BOOK: Beginning ASP.NET 4 : in C# and VB 3 April 16th, 2012 03:15 PM
Need help with SqlDataSource control binici ASP.NET 2.0 Basics 1 February 5th, 2007 05:32 PM
SqlDataSource control error binici ASP.NET 2.0 Basics 1 January 19th, 2007 08:02 PM





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