D4NieLDev D4NieLDev - 2 years ago 85
SQL Question

C# INSERT INTO isn`t working (web sql)

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:

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.

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download