error in insert into satatement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janethy
    New Member
    • Sep 2018
    • 1

    error in insert into satatement

    hi
    I created a button to save the details in ms_access database with oledb but when I run my project and click save button I get an error message saying that "error in insert into statement" any help please.
    here is my code

    Private Sub Button1_Click(s ender As Object, e As EventArgs) Handles Button1.Click
    provider = "Provider=Micro soft.ACE.OLEDB. 12.0;Data Source="
    datafile = "C:\Users\user\ Documents\Datab ase51111.accdb"
    connString = provider & datafile
    myConnection.Co nnectionString = connString
    myConnection.Op en()
    Dim str As String
    'str = "insert into files([number],[fname],[from],[to],[date],[recom])Values(?,?,?,? ,?)"
    str = "INSERT INTO allocate ([IDE],[IT],[SE],[ITE],[DATEE],[FROM],[TO],[DE],[ASSTE],[QUA],[RETE],[RE],[DAM],[STAT]) Values (?,?,?,?,?,?,?, ?,?,?,?,?,?,?)"
    Dim cmd As OleDbCommand = New OleDbCommand(st r, myConnection)
    cmd.Parameters. Add(New OleDbParameter( "IDE", CType(TextBox1. Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "IT", CType(TextBox2. Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "SE", CType(TextBox3. Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "ITE", CType(TextBox4. Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "DATEE", CType(DateTimeP icker1.Value, String)))
    cmd.Parameters. Add(New OleDbParameter( "FROM", CType(TextBox6. Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "TO", CType(TextBox7. Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "DE", CType(TextBox8. Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "ASSTE", CType(DateTimeP icker2.Value, String)))
    cmd.Parameters. Add(New OleDbParameter( "QUA", CType(TextBox10 .Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "RETE", CType(DateTimeP icker3.Value, String)))
    cmd.Parameters. Add(New OleDbParameter( "RE", CType(TextBox12 .Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "DAM", CType(TextBox13 .Text, String)))
    cmd.Parameters. Add(New OleDbParameter( "STAT", CType(TextBox14 .Text, String)))
    Try
    cmd.ExecuteNonQ uery()
    cmd.Dispose()
    myConnection.Cl ose()
    Me.TextBox1.Tex t = ""
    Me.TextBox2.Tex t = ""
    Me.TextBox3.Tex t = ""
    Me.TextBox4.Tex t = ""
    Me.TextBox6.Tex t = ""
    Me.TextBox7.Tex t = ""
    Me.TextBox8.Tex t = ""
    Me.TextBox10.Te xt = ""
    Me.TextBox12.Te xt = ""
    Me.TextBox13.Te xt = ""
    Me.TextBox14.Te xt = ""
    'Exit Sub
    'End With
    Catch ex As Exception
    MsgBox(ex.Messa ge, MsgBoxStyle.Cri tical)
    End Try


    Me.Hide()
    frmmain.Show()


    End Sub
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    I see a couple of things wrong here.

    1) The question marks in your string should be parameters that you are replacing if the string is built the way you're trying to do so. That being said, it appears that you're trying to insert values? I'm not really sure, sorry.

    2) The command you're using to add parameter values should be ".AddWithValue" , not ".Add" when building a string in this manner.

    I've included a couple of examples that would be more accurate.

    This example builds the string in the same fashion that you're trying:
    Code:
            Dim str As String
            str = "INSERT INTO allocate([IDE],[IT],[SE],[ITE],[DATEE],[FROM],[TO],[DE],[ASSTE],[QUA],[RETE],[RE],[DAM],[STAT])VALUES(@IDE,@IT,@SE,@ITE,@DATEE,@FROM,@TO,@DE,@ASSTE,@QUA,@RETE,@RE,@DAM,@STAT)"
            Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
            cmd.Parameters.AddWithValue(New OleDbParameter("@IDE", TextBox1.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@IT", TextBox2.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@SE", TextBox3.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@ITE", TextBox4.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@DATEE", DateTimePicker1.Value.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@FROM", TextBox6.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@TO", TextBox7.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@DE", TextBox8.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@ASSTE", DateTimePicker2.Value.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@QUA", TextBox10.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@RETE", DateTimePicker3.Value.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@RE", TextBox12.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@DAM", TextBox13.Text.ToString()))
            cmd.Parameters.AddWithValue(New OleDbParameter("@STAT", TextBox14.Text.ToString()))
    This example is using a manually built string. Obviously I've only used two textboxes to simulate what would need to be done:
    Code:
            Dim str As String
            str = "INSERT INTO allocate([IDE],[IT],[SE],[ITE],[DATEE],[FROM],[TO],[DE],[ASSTE],[QUA],[RETE],[RE],[DAM],[STAT])VALUES(" & TextBox1.Text.ToString() & "," & TextBox2.Text.ToString() & ")"
    This example is using string variables. Same concept as the previous option but a little more clean. Again, I only used two variables:
    Code:
            Dim str As String
            Dim IDE As String = TextBox1.Text
            Dim IT As String = TextBox2.Text
            str = "INSERT INTO allocate([IDE],[IT],[SE],[ITE],[DATEE],[FROM],[TO],[DE],[ASSTE],[QUA],[RETE],[RE],[DAM],[STAT])VALUES(" & IDE & "," & IT & ")"
    I hope this helps!

    Comment

    Working...