'creating registration page in C#
This code is causing an error:
Incorrect syntax near the keyword 'table'
This error occurs on line 31:
cmd.ExecuteNonQuery();
This line gets highlighted what changes should I make in registration form
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class registration_page : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
{
con.Open();
SqlCommand cmd = new SqlCommand(@"insert into dbo.table(first name, last name, email, password, confirm password, gender) values ('" + Username.Text + "','" + Password.Text + "','" + contact.Text + "','" + gender.Text + "','" + email.Text + "' ,);");
Username.Text = "";
Password.Text = "";
contact.Text = "";
gender.Text = "";
email.Text = "";
// Assign the connection to command
cmd.Connection = con;
// Change the command variable name here
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Solution 1:[1]
There are different issues in your command that will likely lead to problems like "incorrect syntax" errors.
- The ; at the end of your command should not be required
- The "dbo." should not be required
- Have a look on this part of your command: email.Text + "' ,) -> The comma seems to be incorrect.
I recommend to copy the whole command text from the exception, try to execute it directly in your DB management tool and do the necessary modifications there. When it is executed correctly, apply it to your source code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jonas Metzler |
