“ ID”,“ Number”,“ Station”附近的语法不正确

时间:2019-01-15 13:30:26

标签: asp.net

我无法注册获得该帐户的

  

“ ID”,“ Number”,“ Station”附近的语法不正确

  • 前端:ASP.NET
  • 后端:SQL Server

我的代码:

protected void btnReg_Click(object sender, EventArgs e)
{
    try
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        con.Open();

        string query = "INSERT INTO [Table] (Name, Mail ID, Password, Gender, Address, Select District, Pincode, Mobile Number, Qualification, Adhar Number, ID Proof, ID Proof Number, Select Nearest Police Station) 
                        VALUES (@name, @mailid, @password, @gender, @address, @selectdistrict, @pincode, @mobilenumber, @qualification, @adharnumber, @idproof, @idproofnumber, @selectnearestpolicestation)";

        SqlCommand com = new SqlCommand(query, con);

        com.Parameters.AddWithValue("@name", name.Text);
        com.Parameters.AddWithValue("@mailid", email.Text);
        com.Parameters.AddWithValue("@password", password.Text);
        com.Parameters.AddWithValue("@gender", gender.SelectedItem.ToString());
        com.Parameters.AddWithValue("@address", address.Text);
        com.Parameters.AddWithValue("@selectdistrict", district.SelectedItem.ToString());
        com.Parameters.AddWithValue("@pincode", pincode.Text);
        com.Parameters.AddWithValue("@mobilenumber", mobnum.Text);
        com.Parameters.AddWithValue("@qualification", qualification.SelectedItem.ToString());
        com.Parameters.AddWithValue("@adharnumber", adharnum.Text);
        com.Parameters.AddWithValue("@idproof", idproof.Text);
        com.Parameters.AddWithValue("@idproofnumber", idproofnum.Text);
        com.Parameters.AddWithValue("@selectnearestpolicestation", policestation.SelectedItem.ToString());

        com.ExecuteNonQuery();

        Response.Redirect("User.aspx");
        Response.Write("Registration successful");
        con.Close();
    }
    catch (Exception ex)
    {
        Response.Write("Error:" +ex.ToString());
    }
}

1 个答案:

答案 0 :(得分:2)

您的插入查询中的列名不能包含空格-例如“邮件ID”或“手机号码”。

如果您的表中确实有这样的列(确实是设计选择,应该避免!),那么必须将这些列名称括在方括号中-像这样:

string query = 
    "INSERT INTO [Table] (Name, [Mail ID], Password, Gender, Address, 
                          [Select District], Pincode, [Mobile Number], Qualification, 
                          [Adhar Number], [ID Proof], [ID Proof Number],
                          [Select Nearest Police Station]) 
     VALUES (@name, @mailid, @password, @gender, @address, 
             @selectdistrict, @pincode, @mobilenumber, @qualification, 
             @adharnumber, @idproof, @idproofnumber, @selectnearestpolicestation)";

另外,使用T-SQL保留关键字Table作为表或列名称是另一个非常糟糕的命名选择-尝试避免这种情况,这只是麻烦和头痛的秘诀-而且选择不当名称也是如此,因为它实际上并不传达表包含的 数据类型。