You need to add quotes around your parameters when assigning a value:
sqlCommand.Parameters["@email"].Value = email;
sqlCommand.Parameters["@username"].Value = userName;
The reason why you need quotes is because in your case @email
and email
refers to the same string variable. You can read more about @
symbol in C# in answers to What's the use/meaning of the @ character in variable names in C#?
I'm not 100% sure it code above works or not, if not - you then you should use method Add
or AddWithValue
. Regardless of the method you should use "@email"
instead of @email
.
For recommendation why to use Add
in favor of AddWithValue
please refer to this article.
EDIT: It seems the issue is different. In your code you assign email
and username
to null
:
string email = null, userName = null;
Then nowhere in the code you change these values, i.e. they still remain null
. Then when you get to this line:
if (email != null && userName != null)
your values are still null
, and the logic inside if
statement is never executed. Fix this part of the code first, and then my initial answer will become helpful as well.
UPDATE 2 SQL Parameters collection does not contain the parameters, so it needs to be performed using Add
method:
sqlCommand.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = email;
sqlCommand.Parameters.Add("@userName", SqlDbType.VarChar, 12).Value = userName;