Click here to Skip to main content
11,719,185 members (84,116 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Access VB.NET MS-Access , +
I'm always getting this error since i added a couple of column in the tblapplicants table. NOTE: I'm using ms access and the data type i'm using is short text (for testing only)

Here is my code:

Private Sub addtodb()
        Dim querytodb As String
        querytodb = String.Format("INSERT INTO tblapplicants (program, fname, mname, lname, sex, civilstatus, address, highschoolatt, highschooladd, colunivatt, colunivadd, course, companyname, workexpyrs, degree, birthday, birthplace, nationality, religion, language, mobno, telno, emailadd, yrgraduatedhs, yrgraduatedcoluniv) Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}')", _
                                  programcb.Text, fnametb.Text, mnametb.Text, lnametb.Text, sexcb.Text, cscb.Text, addresstb.Text, hstb.Text, hsatb.Text, cutb.Text, cuatb.Text, coursetb.Text, noctb.Text, yearswecb.Text, degreefcb.Text, bdtp.ToString, birthplacetb.Text, nationalitytb.Text, religiontb.Text, languagetb.Text, mobilenotb.Text, telnotb.Text, eaddtb.Text, graduatedhstb.Text, graduatedcoltb.Text)
        Try
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(querytodb, conn)
            cmd.ExecuteNonQuery()
        Catch ex As OleDbException
            MsgBox("Database Insertion Error!" + vbNewLine + ex.ToString & vbCrLf, MsgBoxStyle.Critical, Title:="Database Error!")
            'error logging
            My.Computer.FileSystem.WriteAllText(Application.StartupPath + "_error_log.txt", ex.ToString, True)
            'MsgBox("Database Insertion Error!", MsgBoxStyle.Critical, Title:="Database Error!")
        Finally
            conn.Close()
            'MsgBox("Student Successfully Added!")
        End Try
    End Sub

Here is the error:

System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement.
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   at eteeap.AddApplicant.addtodb() in C:\Users\anonsec\Documents\Visual Studio 2010\Projects\eteeap\eteeap\AddApplicant.vb:line 15

The line 15 in my code is the cmd.ExecuteNonQuery()

Thanks for the help. I hope you'll help me. Thanks Guys.
Posted 7-Dec-12 15:31pm
v3
Comments
@AmitGajjar at 7-Dec-12 22:04pm
   
you do not need single quotation when passing parameter.
joenel of ph at 7-Dec-12 23:58pm
   
What do you mean sir. But it works while ago. Is there another method of inserting into the database programmatically?
@AmitGajjar at 8-Dec-12 3:23am
   
use stored procedure. it will be easier to maintain
Krunal Rohit at 8-Dec-12 0:08am
   
For inserting values, you've defined 2 closing braces and only one opening brace... Please make it correct..
joenel of ph at 8-Dec-12 0:40am
   
I cant get it. what line number is it. Thanks for the help :(
Krunal Rohit at 8-Dec-12 0:41am
   
Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}')", _
programcb.Text, fnametb.Text, mnametb.Text, lnametb.Text, sexcb.Text, cscb.Text, addresstb.Text, hstb.Text, hsatb.Text, cutb.Text, cuatb.Text, coursetb.Text, noctb.Text, yearswecb.Text, degreefcb.Text, bdtp.ToString, birthplacetb.Text, nationalitytb.Text, religiontb.Text, languagetb.Text, mobilenotb.Text, telnotb.Text, eaddtb.Text, graduatedhstb.Text, graduatedcoltb.Text)
joenel of ph at 8-Dec-12 0:48am
   
Is it already correct? i can't insert this always shows INSERT INTO error even i change it :(
Krunal Rohit at 8-Dec-12 0:41am
   
here..

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Can you verify whether any of the field values are exceeding the maximum allowed size in database? Please double check table structure and the value passed for insert.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 705
1 OriginalGriff 285
2 F-ES Sitecore 180
3 Jochen Arndt 160
4 Richard MacCutchan 110
0 Sergey Alexandrovich Kryukov 1,679
1 OriginalGriff 610
2 Maciej Los 529
3 Richard MacCutchan 460
4 F-ES Sitecore 455


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 9 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100