p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   Chapter 14 problem (http://p2p.wrox.com/showthread.php?t=86483)

Nick Makin January 28th, 2012 10:10 AM

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

Imar January 28th, 2012 03:18 PM

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

Imar January 28th, 2012 03:24 PM

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

Nick Makin January 29th, 2012 11:03 AM

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

Imar January 29th, 2012 11:17 AM

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

Nick Makin January 31st, 2012 01:55 PM

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

Imar January 31st, 2012 02:00 PM

Did you try to debug and look at e.Exception?

Imar

Nick Makin February 13th, 2012 09:03 AM

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

Imar February 13th, 2012 09:11 AM

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

Nick Makin February 13th, 2012 09:32 AM

No i dont think so, ive just followed the steps in the book. Is it worth going back and re-doing the connection strings?

Imar February 13th, 2012 09:35 AM

Can you post the code for your connection strings in web.config?

Imar

Nick Makin February 13th, 2012 09:52 AM

Ive tried to open up the Database in Database Explorer, and it wont let me open it. Shall I delete it and re-add the database ?

Nick Makin February 13th, 2012 09:53 AM

Yeah ofcourse here it is:

Code:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
    <add name="PlanetWroxConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PlanetWrox.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
    <add name="PlanetWroxEntities" connectionString="metadata=res://*/App_Code.PlanetWrox.csdl|res://*/App_Code.PlanetWrox.ssdl|res://*/App_Code.PlanetWrox.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\PlanetWrox.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <pages theme="Monochrome">
      <controls>
        <add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx" />
      </controls>
    </pages>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
      <buildProviders>
        <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" />
      </buildProviders>
    </compilation>
  </system.web>
  <system.net>
    <mailSettings>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>


Imar February 13th, 2012 10:04 AM

Hmmm, odd, as it looks fine to me. Yeah, see what happens when you try to recreate them. Can you still log in to SQL Server (at .\SqlExpress) using Management Studio?

Also, you may want to edit your post and remove your Gmail account data, and change your Gmail password to avoid abuse....

Imar

Nick Makin February 13th, 2012 10:25 AM

Ha oops, thanks for that Imar, removed it!

Ill try and change the database's around and ill post back if i have any other problems,

thanks again,

Nick

Nick Makin February 13th, 2012 01:40 PM

Bit confused, gone into the Resources folder in my C drive, then into chapter 12 where the PlanetWrox database is and tried to copy the database into my App_Data folder and tried to open it and im getting the same error.

Am I going about it the wrong way?

Thanks,

Nick

Imar February 13th, 2012 01:52 PM

Quote:

Can you still log in to SQL Server (at .\SqlExpress) using Management Studio?
Can you?

Cheers,

Imar

Nick Makin February 14th, 2012 11:40 AM

That doesent sound familiar, dont think ive used that before?

Nick

Imar February 14th, 2012 12:48 PM

It can be installed using the Web Platform Installer (see chapter 1). If it is installed, you'll find it in the Windows Start Menu with a name like SQL Server Management Studio.

Imar

Nick Makin February 15th, 2012 12:30 PM

Quote:

Originally Posted by Imar (Post 281454)
Can you?

Cheers,

Imar

Yes I can Imar, but in the list of databases the PlanetWrox.mdf isnt there if that means anything? Ive tried to open that database normally from my C drive but doesent work.

Nick

Imar February 15th, 2012 12:36 PM

I don't understand it. It all looks good and exactly as it should. Your EF model uses a connection string that points to .\SqlExpress which in turn you can access. I really have no idea why this doesn't work.

Maybe removing the EF model (and the associated connection string from the web.config file) and the adding them again sheds some light?

Imar

Imar February 15th, 2012 12:40 PM

Quote:

but in the list of databases the PlanetWrox.mdf isnt there if that means anything?
Yeah, that makes sense and is expected. Using AttachDbFilename in the connection string the database is atatched on the fly and doesn't appear in SQL Server's database list.

Cheers,

Imar

Nick Makin February 15th, 2012 12:43 PM

Sorry for the confusion but I dont think I log in using SQL Server. When I was in the sql server management I could see any databases there and PlanetWrox.mdf wasnt there. Now I tried to modify the database connection in VWD, used sql server authentication and enter the details and go but it didnt let me log in.

Thanks for all the help Imar,

Nick

Imar February 15th, 2012 12:47 PM

Quote:

used sql server authentication and enter the details and go but it didnt let me log in.
And what details did you enter? No need to post the actual user name or password, but wondering how you got hold of this account. Notmally, Express uses Windows Authentication by default.

Did you use the same sql server account to log into Management Studio?

Imar

Nick Makin February 15th, 2012 12:47 PM

Wow, ive removed the connection and completely re-done it and its connected! I dont have a clue why it wasnt working before because none of its details like the address was changed, or it should of worked when I tried to re-do it previously. Im totally suprised that its working now!

I can now expand the database and can see the Genre and Review tables.

Thanks for the help Imar, so pleased I can now move on after all this.

Cant thank you enough,

Nick

Imar February 15th, 2012 04:12 PM

Good to hear. Hopefully this now also fixes your original problem with the Manage PhotoAlbum page....

Cheers,

Imar

Nick Makin February 22nd, 2012 12:10 PM

No still got the same problems but theyve got worse. I think modifying my database connection was a bad idea because know i cant find the orginal database file that i have been working with. My PlanetWrox.edmx reflects the database i had but i cant find the database for some reason which had all of my reviews and genres in, so with the PlanetWrox.edmx i can make a database from the relationships and do it that way.

Also then ill have to change the PlanetWroxEntities and PlanetWroxConnectionString1

Once all that is changed do you think that my project should be working again or do you think by doing this im just messing more things up because at the moment its all just really confusing me and want to get back on track.

Thanks,

Nick

Imar February 22nd, 2012 02:03 PM

Not sure how to answer that. Yes, it should fix all your problems once you configured everything correctly. But since I am a bit confused about what's not working, I can't recommend how to fix it.

Losing the database sounds bad, but I can't really recommend a way to get it back ;-) You can always grab the code and database from the previous chapter and use that as a starting point....

Imar

natsuto February 24th, 2012 01:19 AM

See what happens when you try to re-create them. You can also log on to SQL Server Management Kodak 10 Studio?

Nick Makin February 26th, 2012 10:29 AM

Quote:

Originally Posted by Imar (Post 281778)
Not sure how to answer that. Yes, it should fix all your problems once you configured everything correctly. But since I am a bit confused about what's not working, I can't recommend how to fix it.

Losing the database sounds bad, but I can't really recommend a way to get it back ;-) You can always grab the code and database from the previous chapter and use that as a starting point....

Imar

Ever since I found out my database was gone I have been using the databases from the source code, but every time I log into VWD I have to modify the connection to the database again. Ill try and re-do the connection strings to another database and see what happens,

Nick

Nick Makin February 26th, 2012 10:54 AM

After all this time trying to get this to work ive decided that im going to cheat, its taking up too much time and stress wondering why this isnt working. So ive made a new site, and copied all the folders from the source code folders for chapter 15 so im back where I was, but in a new site.

Thank you Imar for your help!

Nick


All times are GMT -4. The time now is 12:36 PM.

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