发现错误,我无法找到它帮助我

时间:2016-08-14 10:29:52

标签: sql asp.net sql-server

我无法在我的代码中找到该错误

  

错误:System.Data.SqlClient.SqlException(0x80131904):不正确   'Name'附近的语法。

try
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["UserData_DB"].ConnectionString);

        string insert = "INSERT INTO UserData(First Name,Last Name,Father Name,CNIC NO,Gender,Religion,Address,City, Cell Number, Email) VALUES (@fName,@lName,@fathName,@cnicNo,@gender,@religion,@address,@city,@cellNumber,@email)";
        SqlCommand com = new SqlCommand(insert, con);
        com.Parameters.AddWithValue("@fName", TextBoxfName.Text);
        com.Parameters.AddWithValue("@lName", TextBoxlName.Text);
        com.Parameters.AddWithValue("@fathName", TextBoxFatherName.Text);
        com.Parameters.AddWithValue("@cnicNo", TextBoxcnicNo.Text);
        com.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedItem.Value.ToString());
        com.Parameters.AddWithValue("@religion", RadioButtonList2.SelectedItem.Value.ToString());
        com.Parameters.AddWithValue("@address", TextBoxAddress.Text);
        com.Parameters.AddWithValue("@city", DropDownList1.SelectedValue.ToString());
        com.Parameters.AddWithValue("@cellNumber", TextBoxCellNumber.Text);
        com.Parameters.AddWithValue("@email", TextBoxEmail.Text);

        con.Open();
        com.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception ex)
    {
        Response.Write("Error: " + ex.ToString());
    }

2 个答案:

答案 0 :(得分:1)

插入中的列名有空格,如果列名有空格,则需要用方括号[]括起来,如下所示:

INSERT INTO Names([First Name],[Last Name])VALUES('First Name 1','Last Name 1')

理想情况下,SQL中的列名不应包含空格,我建议将其重命名为FirstNameLastName e.t.c

答案 1 :(得分:0)

你必须在[]

中添加包含空格的属性
"INSERT INTO UserData([First Name],[Last Name],[Father Name],CNIC NO,Gender,Religion,Address,City, Cell Number, Email) VALUES (@fName,@lName,@fathName,@cnicNo,@gender,@religion,@address,@city,@cellNumber,@email)";