 |
BOOK: Beginning ASP.NET 4 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4 : 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
|
|
|
|
|

January 28th, 2012, 10:10 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
Chapter 14 problem
Hi, im currently working through chapter 14 and im on step 16 on the exercise on 548. I have added a new photo album to my site using the NewPhotoAlbum.aspx and now ManagePhotoAlbum.aspx opens, whenver i try and enter any text into this page and press "Insert" an error gets thrown...
The title of the error is "Object reference not set to an instance of an object"
and there is a line of code highlighed in my ManagePhotoAlbum.cs file...
Code:
myPicture.PhotoAlbumId = photoAlbumId
Not sure what the error means, checked my code and I cant find any silly errors in it so not sure what the problem is.
Any help would be appreciated,
Nick
|
|

January 28th, 2012, 03:18 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
This typically happens when the ID of the new album is not correctly sent to the Manage page.
Can you post the code for the New and Manage pages? If you post code here, please copy it to Notepad first to remove color coding and then use code tags (the # on the toolbar for the forum post editor) to wrap the code.
Cheers,
Imar
|
|

January 28th, 2012, 03:24 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
This usually means that the ID of the new album is not passed correctly to the Manage page. Can you post the code for the New and Manage pages? If you post code, please copy it to Notepad first to remove color coding and then wrap it in Code tags (using the # button on the toolbar).
Cheers,
Imar
|
|

January 29th, 2012, 11:03 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
Hi Imar, thanks again for the response.
Here is the code for NewPhotoAlbum.cs
Code:
protected void EntityDataSource1_Inserted(object sender, EntityDataSourceChangedEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
myPhotoAlbum.Id.ToString()));
}
and the ManagePhotoAlbum.cs
Code:
protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"));
Picture myPicture = (Picture)e.Entity;
myPicture.PhotoAlbumId = photoAlbumId;
FileUpload FileUpload1 =
(FileUpload)ListView1.InsertItem.FindControl("FileUplaod1");
string virtualFolder = "~/GigPics/";
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));
myPicture.ImageUrl = virtualFolder + 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;
}
Sorry for the late reply,
Nick
|
|

January 29th, 2012, 11:17 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
A few things to try:
1. (FileUpload)ListView1.InsertItem.FindControl("File Uplaod1");
Is the control called FileUplaod1? Or FileUpload1?
2. Try debugging and look at e.Exception inside the EntityDataSource1_Inserting method. Maybe another error has occurred?
3. If this doesn't help, please post the full code including the markup of these pages so I can try it out myself.
Cheers,
Imar
|
|

January 31st, 2012, 01:55 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
Hi Imar, thanks for your reply and apologies for my late one.
I have made the spelling correction to my code and ran it, entered a new album and pressed the button. Then it throws the error: "Object reference not set to an instance of an object" for the code:
Code:
Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
Let me know if you want to post the markup to these 2 pages,
Thanks Imar,
Nick
|
|

January 31st, 2012, 02:00 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Did you try to debug and look at e.Exception?
Imar
Last edited by Imar; January 31st, 2012 at 02:38 PM..
|
|

February 13th, 2012, 09:03 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
Sorry for your really late reply, I have tried to debug the EntityDataSource1_Inserting method in the ManagePhotoAlbum.cs and I got this error straight away in my browser:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
Thanks,
Nick
|
|

February 13th, 2012, 09:11 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I figured it would be something like that. Looks like EF can't access your database. Did you make any changes to your connection string for EF and/or SQL Server?
Imar
|
|

February 13th, 2012, 09:32 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
No i dont think so, ive just followed the steps in the book. Is it worth going back and re-doing the connection strings?
|
|
 |
|