Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 29th, 2018, 12:23 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2018
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default Chapter 14 ListView Control "TRY IT OUT" step 10

I am working on part"Try It Out Inserting and Deleting Data with the ListView Control",page 513-518.
I am following the instruction at step 10, close all open files, and then request "NewPhotoAlbum.aspx", but when enter the description , the tooltip and select URL for image in "ManagePhotoAlbum.aspx", I get an error

Server Error in '/' Application.
A null value for parameter 'photoAlbumId' of non-nullable type 'System.Int32' for method 'System.Linq.IQueryable ListView1_GetData(Int32)' in '_ManagePhotoAlbum'. An optional parameter must be a reference type or a nullable type.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: A null value for parameter 'photoAlbumId' of non-nullable type 'System.Int32' for method 'System.Linq.IQueryable ListView1_GetData(Int32)' in '_ManagePhotoAlbum'. An optional parameter must be a reference type or a nullable type.

Here is my code for "NewPhotoAlbum.aspx":
Code:
<%@ Page Title="Create New Photo Album" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" 
    AutoEventWireup="true" CodeFile="NewPhotoAlbum.aspx.cs" Inherits="_NewPhotoAlbum" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" runat="Server">
  <asp:DetailsView AutoGenerateRows="false" ID="DetailsView1" DefaultMode="Insert" runat="server" 
      InsertMethod="DetailsView1_InsertItem">
    <Fields>
      <asp:BoundField DataField="Name" HeaderText="Name" />
      <asp:CommandField ShowInsertButton="True" ShowCancelButton="false" />
    </Fields>
  </asp:DetailsView>
</asp:Content>
code for "NewPhotoAlbum.aspx.cs":
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }

    public void DetailsView1_InsertItem()
    {
        PhotoAlbum photoAlbum = new PhotoAlbum();
        TryUpdateModel(photoAlbum);
        if (ModelState.IsValid)
        {
            using (var myEntities = new PlanetWroxEntities2())
            {
                myEntities.PhotoAlbums.Add(photoAlbum);
                myEntities.SaveChanges();
            }
            Response.Redirect(string.Format("ManagePhotoAlbum?PhotoAlbumId={0}", photoAlbum.Id.ToString()));
        }
    }
}
code for "ManagePhotoAlbum.aspx"
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true"
    CodeFile="ManagePhotoAlbum.aspx.cs" Inherits="ManagePhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" runat="Server">
  <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" InsertItemPosition="LastItem" 
      SelectMethod="ListView1_GetData" InsertMethod="ListView1_InsertItem" DeleteMethod="ListView1_DeleteItem" ItemType="Picture">
    <InsertItemTemplate>
      <li>Description:
        <asp:RequiredFieldValidator ID="reqDesc" ControlToValidate="Description" runat="server" ErrorMessage="Enter a description." />
        <asp:TextBox ID="Description" runat="server" TextMode="MultiLine" Text='<%# BindItem.Description %>' /><br />
        ToolTip:
        <asp:RequiredFieldValidator ID="reqToolTip" ControlToValidate="ToolTip" runat="server" ErrorMessage="Enter a tool tip." />
        <asp:TextBox ID="ToolTip" runat="server" Text='<%# BindItem.ToolTip %>' /><br />
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:CustomValidator ID="cusValImage" runat="server" ErrorMessage="Select a valid .jpg file." />
        <asp:Button ID="InsertButton" runat="server" Text="Insert" CommandName="Insert" />
      </li>
    </InsertItemTemplate>
    <ItemTemplate>
      <li>Description:
        <asp:Label ID="Description" runat="server" Text='<%# Item.Description %>' /><br />
        ToolTip:
        <asp:Label ID="ToolTip" runat="server" Text='<%# Item.ToolTip %>' /><br />
        <asp:Image ID="ImageUrl" runat="server" ImageUrl='<%# Item.ImageUrl %>' /><br />
        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="False" />
      </li>
    </ItemTemplate>
    <LayoutTemplate>
      <ul class="ItemContainer">
        <li runat="server" id="itemPlaceholder" />
      </ul>
    </LayoutTemplate>
  </asp:ListView>
</asp:Content>

code for "ManagePhotoAlbum.aspx.cs"
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.ModelBinding;

public partial class ManagePhotoAlbum : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    public IQueryable ListView1_GetData([QueryString("PhotoAlbumId")] int photoAlbumId)
    {
        var myEntities = new PlanetWroxEntities2();
        return from p in myEntities.Pictures
               where p.PhotoAlbumId == photoAlbumId
               select p;
    }

    public void ListView1_InsertItem([QueryString("PhotoAlbumId")] int photoAlbumId)
    {
        Picture picture = new Picture();
        TryUpdateModel(picture);
        if (ModelState.IsValid)
        {
            // Save changes here
            using (var myEntities = new PlanetWroxEntities2())
            {
                picture.PhotoAlbumId = photoAlbumId;
                myEntities.Pictures.Add(picture);
                myEntities.SaveChanges();
            }
        }
    }


    public void ListView1_DeleteItem(int id)
    {
        using (var myEntities = new PlanetWroxEntities2())
        {
            var picture = (from p in myEntities.Pictures
                           where p.Id == id
                           select p).Single();

            myEntities.Pictures.Remove(picture);
            myEntities.SaveChanges();
        }
    }
}
I am using VS2017 Community. Can anyone help me? Thank you!

Last edited by oldseven; August 29th, 2018 at 12:27 PM.
Reply With Quote
  #2 (permalink)  
Old August 29th, 2018, 04:15 PM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Hi there,

I compared your code with mine and it looks pretty much the same.

When you create a new album, does it get saved in the database correctly? Also, after creating the album you are taken to ManageAlbum.aspx. Does the URL for that page have the ID in its query string? Can you post the full URL for that page after creating the album?

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!
Reply With Quote
  #3 (permalink)  
Old August 29th, 2018, 10:45 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2018
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default URL

Hi, Imar
Thank you for replaying me!
Here is the URL after enter the album name and click insert in "NewPhotoAlbum"
URL"http://localhost:57197/ManagePhotoAlbum?PhotoAlbumId=24"
but after enter the description , tooltip and select a random picture in my local computer, click insert , I get the error

Thank you
Reply With Quote
  #4 (permalink)  
Old August 29th, 2018, 11:52 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2018
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default There might be a problem with SQL Server 2012 connection

Hi, Imar

There is also a problem after I create the "PlanetWrox" database in SQL Server 2012 and save it at "C:\BegASPNETCS\Site\App_Data\PlanetWrox.mdf" . I try to connect the "PllanetWrox.mdf" in Visual Studio , but I get the error
"Unable to open the physical file "C:\BegASPNETCS\Site\App_Data\PlanetWrox.mdf". Operating system error 5: "5(Access is denied.)".
An attempt to attach an auto-named database for file C:\BegASPNETCS\Site\App_Data\PlanetWrox.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

Then I have to delete the "PlanetWrox" database in SQL Server 2012 and copy the "PlanetWrox.mdf " download source code into
"C:\BegASPNETCS\Site\App_Data" to make it work.
It looks like there is a connection error between Visual Studio and SQL Server 2012, I am working on my own PC, is there any advice about this?

Thank you very much!

Last edited by oldseven; August 30th, 2018 at 12:02 AM.
Reply With Quote
  #5 (permalink)  
Old August 30th, 2018, 03:17 AM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

I am not seeing what the issue is from the code you posted. Can you make a zipped copy of the site and database available somewhere so I can take a look?

You can't have SQL Server connected to the database and still open it in Visual Studio at the same time as it causes a lock. You could detach it from SQL Server and use VS exclusively, or keep it attached to SQL Server and update your connection string to use Initial Catalog rather than attaching it. Appendix B has more details on connection strings.

BTW: it's better to create a new topic for new issues as we're now discussing missing photoAlbumIds and SQL Server issues in the same topic which can be confusing.

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!
Reply With Quote
  #6 (permalink)  
Old August 30th, 2018, 07:30 AM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2018
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default Zipped site upload on Dropbox

Hi, Imar

I have uploaded the zipped site on Dropbox, here is the shared link:
https://www.dropbox.com/s/rksbvzxgr9...NETCS.zip?dl=0
Please take a look at the site.

Thank you!
Reply With Quote
  #7 (permalink)  
Old August 30th, 2018, 08:16 AM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Looks like you're missing two things:

1. Assigning the ImageUrl to the picture. There's code in the book that shows how to do that from the uploaded image. For now, this should suffice:

picture.ImageUrl = "Test";
picture.PhotoAlbumId = photoAlbumId;
myEntities.Pictures.Add(picture);
myEntities.SaveChanges();

2. Saving the entity. Remove the comments from this line: myEntities.SaveChanges();

With those changes in place I can add images to an album created by NewPhotoAlbum.

Note: when you're in VS, editing ManagePhotoAlbum and hit F5 you load that page directly which results in the null error you posted earlier. That page requires a PhotoAlbumId to be present in the query string.

Hope this helps,

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!
Reply With Quote
  #8 (permalink)  
Old August 30th, 2018, 11:56 AM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2018
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default It works well now

Hi, Imar

I followed your instruction. The site works well now. But the piece of code
"picture.ImageUrl = "Test";" is not in the book. I guess most of the readers get this part working without adding this piece of code.
So, what is problem in my code? Could you explain it?

Thank you very much!
Reply With Quote
  #9 (permalink)  
Old August 30th, 2018, 12:30 PM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

When you follow the book, you have this in your template:

ImageUrl: <asp:TextBox ID="ImageUrl" runat="server"
Text='<%# Bind("ImageUrl") %>' /><br />

That inserts an empty value for the image URL which then prevents the error from happening. You went one step ahead by adding the FileUpload, replacing the ImageUrl without finishing the exercise with the code that assigns ImageUrl a proper value (page 527).

Hope this helps,

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!
Reply With Quote
  #10 (permalink)  
Old August 30th, 2018, 12:58 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2018
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default Thank you very much!

Hi, Imar

Thank you very much for the answer!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
(P. 515) CustomValidator in "Customizing Templates of the Listview Control" Tulsi BOOK: Beginning ASP.NET 4 : in C# and VB 1 November 2nd, 2012 07:08 AM
Chapter 14 "Uploading Images" tariq BOOK: Beginning ASP.NET 4 : in C# and VB 1 September 19th, 2010 08:01 AM
Chapter 16-14 "...WebPartZone' does not have a public property named 'StateListBox'" johandavidson BOOK: Professional ASP.NET 4 : in C# and VB 1 July 21st, 2010 11:46 AM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 02:22 AM
Page 417 step 12 "Bound to:" field Dan Cern BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 August 24th, 2009 01:15 PM



All times are GMT -4. The time now is 08:48 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.