2013-02-24 95 views
0

我得到和插入語句中的語法錯誤。 IF語句工作得很好剛開時,它試圖將信息保存這個錯誤語法錯誤在INSERT INTO語句在vb.net

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 
     If txt12Per.Text >= TextBox12.Text And txtGPer.Text >= TextBox11.Text And TextBox1.Text >= TextBox10.Text Then 
      Try 
       'Dim da As OleDb.OleDbDataAdapter 
       Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Taher\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\Database1.accdb;Persist Security Info=False;" 
       Me.con = New OleDb.OleDbConnection() 
       con.ConnectionString = dbprovider 
       con.Open() 

       Dim sqlquery As String = "INSERT INTO MCAscheduled (URno,SName,hsc,gper,pgper,pstatus,cname,hrname,position,hscinter,ginter,pginter,comments)" + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "');" 
       Dim sqlcommand As New OleDb.OleDbCommand(sqlquery) 

       With sqlcommand 
        .CommandText = sqlquery 
        .Connection = con 
        .ExecuteNonQuery() 
        con.Close() 
        txtUrn.Text = "" 
        txt12Per.Text = "" 
        txtGPer.Text = "" 
        txtName.Text = "" 
        cmbNameofGCourse.Text = "" 
       End With 
       MsgBox("Record Added") 
      Catch ex As Exception 
       MsgBox(ex.ToString) 
      End Try 
     Else 
      MsgBox("Student Not eligible for the requested company") 
     End If 
    End Sub 

有人可以幫我這個....

+0

哪個錯誤信息?哪裏? – Sebastian 2013-02-24 12:55:37

+0

得到這個錯誤在第64行是.executenonquery,但說INSERT INTO語句中的語法錯誤 – CrashOverride 2013-02-24 12:59:38

+0

作出的更改仍然得到相同的錯誤..... :(代碼更新 – CrashOverride 2013-02-24 16:46:55

回答

3

您試圖插入13列,但只提供11個值!

+3

有沒有在這裏的回聲?) – Guffa 2013-02-24 13:08:17

3

你有13個領域中插入值,但只有11值。

0

只好位置字段更改爲別的東西不知道爲什麼,但它的工作

Dim sqlquery As String = "INSERT INTO MCAscheduled (URno,sname,hsc,gper,pgper,pstatus,cname,hrname,hscinter,ginter,pginter,comments,post,course,pcourse)" + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "','" & ComboBox1.Text & "','" & cmbNameofGCourse.Text & "','" & TextBox5.Text & "');" 
1

使用括號,它會正常工作。

Dim sqlquery As String = "INSERT INTO MCAscheduled **([URno],[SName],[hsc],[gper],[pgper],[pstatus],[cname],[hrname],[position],[hscinter],[ginter],[pginter],[comments])"** + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "');" 
+1

它會如果你想格式化這個答案,這很好。這是不可讀的。 – 2013-08-02 02:51:54

相關問題