IsakBosman
IsakBosman

Reputation: 1533

Strange Guid.NewGuid() behaviour

I have searched the site for an answer to this question, but I cannot seem to figure this one out.

I have use the NewGuid() method many times and it has worked greate. But now for some reason it creates an empty Guid.

Here is my code:

// Class of the Guid Object
public class CardUserAccount
    {
        // User ID of the user's profile
        public Guid UserId { get; set; }
    }

//Page object where method is called
Public partial class CreateSale : System.Web.UI.UserControl
    {
        // Create the UserProfile object
        public CardUserAccount profile = new CardUserAccount();


    protected void ContinueButton_Click(object sender, EventArgs e)
    {
        Guid _userId = Guid.NewGuid();
        profile.UserId = _userId;
    }

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        // Method to add object to database
        SubmitProfile(profile);
    }

I then call a simple linq to entities method to add the object to the entity object.

I have double checked it and I am not overwriting it anywhere.

However could it be a problem that I am creating the profile object outside of the page_load method. I thought this would not affect the object during postback.

I would appreciate the help

Upvotes: 0

Views: 202

Answers (1)

Roy Dictus
Roy Dictus

Reputation: 33139

Is this actual code? Because you declare and initialize the variable, then do nothing with it.

If you intend to overwrite a field value, you should not declare that field inside this method.

Upvotes: 2

Related Questions