0

Sorry for the poor title, it's really difficult to describe this issue. On my ASPX site, when a user clicks the button, I need to generate two new database items. I firstly need to create my User object, containing things such as email, password, and the UserID which is the primary key for that table. This has been working fine for a long time as it's pretty simple, and I've even set up some other tables with foreign keys referencing the User table (such as Images for Images uploaded by a user). Today I've been beginning to implement the Profile class, which will generate a default profile when a user registers.

The profile class takes 5 parameters to generate:

 User user, 
 string TagLine,
 string AboutMe,
 string ProfilePhoto,
 string ProfilePhotoExtension

Now you need not worry about the last 4 of those, the one I'm having trouble with is the User user. This takes a user class and acts as a foreign key to the User table's primary key (this is so I can retrieve information such as Username to put into the profile.)

Here's the snippet of code which is responsible for generating a new user, and then a profile based on that:

            //Add user
            var newUser = new BO.User(txtEmail.Text, newHashedPassword, txtUsername.Text);
            //Create new profile
            var newProfile = new BO.Profile(newUser, "This user has not yet set their tagline", "This user has not yet set their about me", "default", ".jpg");
            db.UserAdd(newUser);
            db.ProfileAdd(newProfile);
            db.Save();

So as you can see I generate a new user class in the newUser variable, and then I try to use that variable to generate a new profile. I think that is the bit which is generating my error. After that I add and save them both to my database.

The error I'm getting is:

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object. at PiccyPic.BO.Profile..ctor(User user, String tagline, String aboutme, String profilephoto, String profilephotoextension) in c:\Visual Studios\PiccyPic\PiccyPic.BO\Profile.cs:line 37 at PiccyPic.Login.butRegister_Click(Object sender, EventArgs e) in c:\Visual Studios\PiccyPic\PiccyPic\Login.aspx.cs:line 134 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.login_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c03cf19f\4ceaa030\App_Web_mnmhlw3a.14.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Here's a screenshot of the debugging up to the point of when I get the error:

screenshot of the debugging up to the point of when I get the error

If you need any more information please ask for it, I'm not sure what would be helpful in solving this.

1
  • looks like the newUser object is null upon Profile creation, since the other params for Profile are strings that are already present. Just step through the debugger and see which params == null, and fix them. Commented Nov 18, 2014 at 18:50

2 Answers 2

2

If you're using Entity Framework the usual way of structuring this would be for the User class to have a reference to the profile (because the profile belongs to the user):

public Profile UserProfile {get;set;} 

then after you create the user you can set the profile

newUser.UserProfile = new BO.Profile("This user has not yet set their tagline", "This user has not yet set their about me", "default", ".jpg"); //note that we no longer pass a reference to the user
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer. So if I did your above code how would that generate in the database, by which I mean how would the User-Profile records be linked?
@Frayt There will be two tables: User and Profile. Your Profile table will have a column called User_Id and a foreign key should be created which will create the link between the two tables.
Excellent, that's how I wanted it to generate. I'll try it and get back to you.
I did the opposite of your code, because yours generated a Profile_Id column on User so I did public User User and then newProfile.User = newUser and that produced the desired result. Thanks for the help!
Bit of a long shot, but do you have any hints as to why when I try to reference User.Username through a Profile instance, the User is null?
|
-1

In terms of what needs to come first, the User or other entries into the database? Which record needs to be finalised before the other one? Also, are you getting the primary key back from the original entry? and mapping this to the new data? Only skimmed so will try and find out what's going on.

3 Comments

This should be a comment not an answer
I see now that you're new to SO. I'm not sure what the procedure is for seeking further information when you don't have enough rep to comment.
Ok, will try and add a comment now, it was my first intention but with no rep I couldn't comment. Maybe newcomers should be able to comment first? It worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.