85

I am trying to create a user using claim identity asp.net I get this error while creating claims identity user.

  ApplicationUser user = new ApplicationUser { 
                        EmailConfirmed = true, 
                        UserName = model.myUser.Email,
                        Email = model.myUser.Email ,
                        PhoneNumber = model.myUser.PhoneNumber,
                        PhoneNumberConfirmed = true,
                        UserImagePath = model.myUser.UserImagePath,
                        FirstName= model.myUser.FirstName,
                        LastName = model.myUser.LastName,
                        DateOfBirth = model.myUser.DateOfBirth,
                        Culture = model.myUser.Culture,
                        Role = model.myUser.Role
                    };

but when the code was

var user= new ApplicationUser { 

                            UserName = model.myUser.Email,
                            Email = model.myUser.Email ,

                        };

it worked perfectly, so i want to know what is wrong

husseinbaalbaki
  • 901
  • 1
  • 6
  • 7

2 Answers2

204

You have a statement (if or while, for example), right before the code you posted, without curly braces.

For example:

if (somethingIsTrue) 
{    
   var user= new ApplicationUser { 
       UserName = model.myUser.Email,
       Email = model.myUser.Email ,
   };
}

is correct, but the code below:

if (somethingIsTrue) 
   var user = new ApplicationUser { 
      UserName = model.myUser.Email,
      Email = model.myUser.Email ,
   };

will result in CS1023: Embedded statement cannot be a declaration or labeled statement.

UPDATE

The reason, according to @codefrenzy, is that the newly declared variable will immediately go out of scope, unless it is enclosed in a block statement, where it can be accessed from.

The compilation will pass in the following cases though.

If you only initialize a new instance of a type, without declaring a new variable:

if (somethingIsTrue) 
   new ApplicationUser { 
       UserName = model.myUser.Email,
       Email = model.myUser.Email ,
   };

or if you assign a value to an existing variable:

ApplicationUser user;

if (somethingIsTrue) 
   user = new ApplicationUser { 
       UserName = model.myUser.Email,
       Email = model.myUser.Email ,
   };
starball
  • 20,030
  • 7
  • 43
  • 238
felix-b
  • 8,178
  • 1
  • 26
  • 36
9

I just had this error, and the fix was to add a curly brace to the if immediately preceding my code, and then remove it again. Visual Studio facepalm OTD.

Mel Padden
  • 983
  • 1
  • 9
  • 21
  • 4
    This answer doesn't deserve the downvotes. This error sometimes occurs when it shouln't (when no new variables are declared). When VS does something unexpected like this, a solution like this can be good fix. For us a restart of VS helped solve this issue. – Martijn Jan 15 '21 at 10:48
  • 1
    Same with me. VS can be haunted by the ghosts of old compilation errors sometimes and only a restart will fix it. – Matthew Lock Jan 25 '21 at 03:28
  • I just closed the file, and reopened it, didn't need to close VS. – Nick M Jan 30 '23 at 00:51