2016-08-14 36 views
0

我无法找到的错误在我的代码发现错误,我无法找到它帮我出

Error: System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '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()); 
    } 
+0

列名不能包含空格!如果你的列中有空格(开头真的是个坏主意),你必须在它们周围使用**方括号:INSERT INTO UserData([First Name],[Last Name],[Father Name])。 ....' –

+0

在sql中的列不应该用两个单词来命名,所以它的first_name或firstName或类似的东西。 – thsorens

回答

1

列名有空格,如果列名有空格,你需要围绕他们用方括号[],就像这样:

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

理想SQL中的列名不能有空格,我建议重新命名他们像FirstNameLastName e.t.c

+0

我明白了,谢谢。 –

0

你必须把attributs塔包含在[]

空间在插入
"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)"; 
相关问题