-1

I looked in the forum a lot and researched for my problem but I couldn`t find something that helped me. I have tried a lot of suggestions from here. I have a site and 3 sign up pages. every page have some information, validators are already set up and I built an SQL Server (Microsoft visual studio community 2015). Here is the code of the first sign up page:

EDIT The problem was that Table is a reserved word, I just had to write [Table] instead of Table in the sqlCommand

Any help will be estimated.

D4NieLDev
  • 260
  • 1
  • 3
  • 14
  • Are you getting any error – Pரதீப் Apr 23 '16 at 15:24
  • No, I am not receiving any errors – D4NieLDev Apr 23 '16 at 15:36
  • You should not be connecting directly to an mdf file. You may not have credentials and you may be writing to the wrong database. Use the database name in the SQL Server. The server already knows the file name when it was attached. You connection string just needs the name of the server. You can add a 'USE database_name' statement to select the correct database (you may have more than one database in the server). – jdweng Apr 23 '16 at 15:52
  • My Database name is Database.mdf... – D4NieLDev Apr 23 '16 at 16:27

1 Answers1

5

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;
Community
  • 1
  • 1
dotnetom
  • 24,551
  • 9
  • 51
  • 54