Add a database entry using another class

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");

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 StudiosPiccyPicPiccyPic.BOProfile.cs:line 37 at PiccyPic.Login.butRegister_Click(Object sender, EventArgs e) in c:Visual StudiosPiccyPicPiccyPicLogin.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:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET Filesrootc03cf19f4ceaa030App_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:

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

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
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » Add a database entry using another class

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录