Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 January 24th, 2009, 09:25 PM
Authorized User
Points: 129, Level: 2
Points: 129, Level: 2 Points: 129, Level: 2 Points: 129, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 32
Thanks: 4
Thanked 0 Times in 0 Posts
Default Chapter 13 Error

I've enjoyed the book and have had little difficulty getting through the "Try It Out" exercises until now in Chapter 13. I've been experiencing an error on the 4th "Try It Out" exercise and I'm not sure why.

I went through the exercise twice to see if I made any errors, but didn't detect any. What's weird is that it initially worked just fine, but then errors began to occur.

At the end of the exercise, you're asked to open NewPhotoAlbum.aspx in your browser which is no problem. I enter a new album name, click the "Insert" link and I'm taken to the "ManagePhotoAlbum.aspx" page without problem.

The first time I did this, I was able to enter two "photographs" without problem. On the third, I got an exception error. Each subsequent time I've tried to create a new album, I can't enter any pictures without getting the exception error. I enter the info in the three text boxes and click on the Insert Button and I get the error.

When the error appears the tab on the browser says this:

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in the database "C:\http://localhost: 1497/ManagePhotoAlbum.aspx?PhotoAlbumId=6"

I know I can simply copy the downloaded form in, but I'd like to understand what is going on if at all possible. Thanks for any help.

The rest of the browser page error information is as follows:


Server Error in '/' Application.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in database "C:\BEGASPNET\SITE\PLANET WROX\APP_DATA\PLANETWROX.MDF", table "dbo.PhotoAlbum", column 'Id'.
The statement has been terminated.


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.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in database "C:\BEGASPNET\SITE\PLANET WROX\APP_DATA\PLANETWROX.MDF", table "dbo.PhotoAlbum", column 'Id'.
The statement has been terminated.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:

[SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in database "C:\BEGASPNET\SITE\PLANET WROX\APP_DATA\PLANETWROX.MDF", table "dbo.PhotoAlbum", column 'Id'.The statement has been terminated.] System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) +1948826 System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) +4844747 System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) +194 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392 System.Data.SqlClient.SqlDataReader.ConsumeMetaDat a() +33 System.Data.SqlClient.SqlDataReader.get_MetaData() +83 System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297 System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954 System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162 System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32 System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior behavior, String method) +141 System.Data.SqlClient.SqlCommand.ExecuteDbDataRead er(CommandBehavior behavior) +12 System.Data.Common.DbCommand.ExecuteReader() +12 System.Data.Linq.SqlClient.SqlProvider.Execute(Exp ression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +975 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll( Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113 System.Data.Linq.SqlClient.SqlProvider.System.Data .Linq.Provider.IProvider.Execute(Expression query) +344 System.Data.Linq.StandardChangeDirector.DynamicIns ert(TrackedObject item) +136 System.Data.Linq.StandardChangeDirector.Insert(Tra ckedObject item) +202 System.Data.Linq.ChangeProcessor.SubmitChanges(Con flictMode failureMode) +236 System.Data.Linq.DataContext.SubmitChanges(Conflic tMode failureMode) +331 System.Data.Linq.DataContext.SubmitChanges() +19 System.Web.UI.WebControls.LinqToSqlWrapper.SubmitC hanges(DataContext dataContext) +9 System.Web.UI.WebControls.LinqDataSourceView.Inser tDataObject(Object dataContext, Object table, Object newDataObject) +83 System.Web.UI.WebControls.LinqDataSourceView.Execu teInsert(IDictionary values) +448 System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +86 System.Web.UI.WebControls.ListView.HandleInsert(Li stViewItem item, Boolean causesValidation) +277 System.Web.UI.WebControls.ListView.HandleEvent(Eve ntArgs e, Boolean causesValidation, String validationGroup) +652 System.Web.UI.WebControls.ListView.OnBubbleEvent(O bject source, EventArgs e) +233 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.ListViewItem.OnBubbleEve nt(Object source, EventArgs e) +112 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.Button.OnCommand(Command EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +166 System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
Reply With Quote
  #2 (permalink)  
Old January 25th, 2009, 12:14 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I don't have the book so I can't see the code, but this is not really an ASP.NET error.

This is an error coming directly from SQL Server and it's basically complaining about the database schema.

Reading that error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in database "C:\BEGASPNET\SITE\PLANET WROX\APP_DATA\PLANETWROX.MDF", table "dbo.PhotoAlbum", column 'Id'.

The most obviously likely cause is that the record you are currently inserting (I would *guess* you are trying to add a picture to an album at that point) has an Id value that does not match any value of dbo.PhotoAlbum.Id. Example: The PhotoAlbum table has (say) albums with Id = 1 and 2 and 3 and you are trying to add a photo to (say) album 6 (since you said that the URL ended with "?PhotoAlbumId=6"). Note that the field in in the "Picture" table (again, I'm guessing as to the table name, but the name of the contraint "FK_Picture_PhotoAlbum" makes this likely) may *NOT* be named "Id". [In fact, in typical MS naming, it probably won't be...more likely something like "PhotoAlbumId".]

So....

First things first, open up a database browser and look in your PhotoAlbum table. *IS* there a record in that table where the ID values is 6??? [Or whatever the "?PhotoAlbumId=" in the problem URL shows you.] If not, clearly that's the root of the problem.

But now fixing it may be harder.

You will need to figure out *WHY* you are getting an invalid PhotoAlbumId in that URL.

So.... DEBUG time. Have you used the DEBUGGER in Visual Studio yet? If so, now's the time to get started. Try to find where that PhotoAlbumId is being generated. It *SHOULD* be getting it from some query where you specify WHICH album to upload the picture to. (e.g., You say "upload to 'Family' album and then you should have a SQL query that finds out that 'Family' is PhotoAlbumId=6.)

Sorry I can't say more; without the code all this is pretty much just educated guessing.
Reply With Quote
  #3 (permalink)  
Old January 25th, 2009, 12:19 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

p.s.: I *suspect* that the reason it worked with the first album and first pictures is that when you created that new album it retrieved the IDENTITY value for the new albumID and *that* worked. But then when you go back and try to add to an existing album, it's not picking up the correct albumID. Pure guesswork, again.

p.p.s.: There *are* other possible reasons in SQL Server for this same error message, but they are at least an order of magnitude less likely than the one I've suggested.
Reply With Quote
  #4 (permalink)  
Old January 25th, 2009, 03:35 AM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,031
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Hi Psyclist,

On top of what Old Pedant said, a few things. First, the error message. Does it really say this:
Quote:
The conflict occurred in the database "C:\http://localhost:1497/ManagePhotoAlbum.aspx?PhotoAlbumId=6"
It seems strange that the database and URL are mixed....

Secondly, can you post your code for the file ManagePhotoAlbum.aspx.cs / ManagePhotoAlbum.aspx.vb? Maybe you're not getting the right ID from the query string?

Finally, can you define "On the third, I got an exception error". That is, do you get an error when you go to that page the third time, or when you stay on the page when you try to insert a number of pictures in succession?

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
  #5 (permalink)  
Old January 25th, 2009, 12:10 PM
Authorized User
Points: 129, Level: 2
Points: 129, Level: 2 Points: 129, Level: 2 Points: 129, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 32
Thanks: 4
Thanked 0 Times in 0 Posts
Default

OK, thank you Old Pedant and Imar for taking the time to respond. I really appreciate it.

I just tried it again.

Quote:
Originally Posted by Imar View Post
Hi Psyclist,

On top of what Old Pedant said, a few things. First, the error message. Does it really say this:
The error message appears in the web page's tab and then there's the lengthy exception error message on the page itself. The one that appears on the tab reads as follows:

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in database "C:\http://localhost:1497/ManagePhotoAlbum.aspx?PhotoAlbumId=7"

As you can see the "PhotoAlbumId" number increased by one (6 to 7). I checked the data in the table and these Ids do exist.

Quote:
Secondly, can you post your code for the file ManagePhotoAlbum.aspx.cs / ManagePhotoAlbum.aspx.vb? Maybe you're not getting the right ID from the query string?
Here's the ManagePhotoAlbum.aspx.vb page:

PartialClass ManagePhotoAlbum
Inherits BasePage
ProtectedSub LinqDataSource1_Inserting(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.LinqDataSourceInsertEven tArgs) Handles LinqDataSource1.Inserting
Dim myPicture As Picture = CType(e.NewObject, Picture)
myPicture.PhotoAlbumId = Convert.ToInt32(Request.QueryString.Get(
"PhotoAlbumId"))
EndSub
End
Class

Quote:
Finally, can you define "On the third, I got an exception error". That is, do you get an error when you go to that page the third time, or when you stay on the page when you try to insert a number of pictures in succession?
First, this only happened on the first time I ran the exercise in the browser upon completing it. I created the new album and was taken to ManagePhotoAlbum where I was able to successfully add two photos to that album. Then, when I attempted to add a third to that album, I got the error. On each subsequent attempt to create an album and add pics, I got the error when attempting to add the very first pic to the new album.

One other thing I've noticed that doesn't look right, is in the <LayoutTemplate> element in the ManagePhotoAlbum markup view code, the <ul class="itemContainer"> looks funny in that there's a squiggly line under "itemContainer." Not sure what that means exactly, but I think there's something wrong, potentially.

Thanks, again.
Reply With Quote
  #6 (permalink)  
Old January 25th, 2009, 12:18 PM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,031
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Can you send me the project you have so far? It's pretty difficult to see what's going on without seeing the full source and database.

You can send me a message through my Profile page, or through my website: http://imar.spaanjaars.com

When I reply, you know my e-mail address and you can send me the (zipped) version of your project.

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
  #7 (permalink)  
Old January 25th, 2009, 12:41 PM
Authorized User
Points: 129, Level: 2
Points: 129, Level: 2 Points: 129, Level: 2 Points: 129, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 32
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Imar, I didn't see a "Personal Message" option on your profile, so I went to your web site and sent an email to you via your Contact Page. Thanks.
Reply With Quote
  #8 (permalink)  
Old January 25th, 2009, 12:58 PM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,031
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Hi again,

Take a look at the database and the diagram you created. The relation between PhotoAlbum and Picture is incorrect. You related PhotoAlbum.Id to Picture.Id. This means that the new Id of a Picture must exist in the PhotoAlbum table... ;-)

Instead, drop and then recreate the relationship between PhotoAlbum.Id and Picture.PhotoAlbumId....

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
  #9 (permalink)  
Old January 25th, 2009, 02:11 PM
Authorized User
Points: 129, Level: 2
Points: 129, Level: 2 Points: 129, Level: 2 Points: 129, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 32
Thanks: 4
Thanked 0 Times in 0 Posts
Default

That's it! Thanks so much, Imar! Terrific book, btw.

In the diagram those relationship lines don't seem to line up to the specific columns, so I didn't pay it much attention when I created the relationship. But now, doing it over, I see in the dialog box you can be sure the two columns are specifically referenced there. So, I learned something very valuable too!

Thanks, again!
Reply With Quote
  #10 (permalink)  
Old January 25th, 2009, 02:15 PM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,031
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

You're welcome. Glad you like the book so much.

And yes, the diagram isn't that helpful when it comes to relationships; the line just points from one table to another, not to a specific column.

Usually I select the relationship, then open the Properties panel and look at the settings there...

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
The Following User Says Thank You to Imar For This Useful Post:
Psyclist (January 25th, 2009)
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
LINQ Error ? Chapter 13 workidoo BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 July 22nd, 2008 09:37 AM
Chapter 13: Error in user.php pherank BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 July 4th, 2008 09:09 PM



All times are GMT -4. The time now is 03:53 PM.


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