Hi,

I am trying to insert values into my local SQL database. Here is my code:

Imports System.Data.SqlServerCe
Public Class AddSensor

' Shared variables  
Dim con As SqlCeConnection = New SqlCeConnection("Data Source=C:\Users\Bryan\documents\visual studio 2010\Projects\SiteSecure v1.1\SiteSecure v1.1\Database1.sdf")
Dim cmd As SqlCeCommand
Dim myDA As SqlCeDataAdapter
Dim myDataSet As DataSet
Dim oForm1 As AddSensor
Dim DataGridView1 As DataGrid


'Binding database table to DataGridView  
Public Sub ShowData()
    cmd = New SqlCeCommand("Select * FROM Sensor Table", con)
    If con.State = ConnectionState.Closed Then con.Open()
    myDA = New SqlCeDataAdapter(cmd)
    myDataSet = New DataSet()
    myDA.Fill(myDataSet, "Sensor Table")
    DataGridView1.DataSource = myDataSet.Tables("Sensor Table").DefaultView
End Sub

Private Sub Cancel_Click(sender As System.Object, e As System.EventArgs) Handles Cancel.Click
    Me.Close()
End Sub

Private Sub Done_Click(sender As System.Object, e As System.EventArgs) Handles Done.Click
    ' Insert record
    cmd = New SqlCeCommand("INSERT INTO Sensor Table (Equipment ID, Location, Supervisor, Alarm Start, Alarm Stop) Values (@Id, @Loc, @Sup, @Astr, @Asto)", con)
    cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@id"
    cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@Loc"
    cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@Sup"
    cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@AStr"
    cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@ASto"
    cmd.Parameters.Item("@id").Value = EquipmentID.Text
    cmd.Parameters.Item("@Loc").Value = Location2.Text
    cmd.Parameters.Item("@Sup").Value = Supervisor.Text
    cmd.Parameters.Item("@Astr").Value = AlarmStart.Text
    cmd.Parameters.Item("@ASto").Value = AlarmStop.Text
    If con.State = ConnectionState.Closed Then con.Open()
    cmd.ExecuteNonQuery()
    ShowData() 'Rebinding to DataGridView and view result 
    Me.Close()

End Sub

End Class

In the "Done_Click" procedure, I keep getting an error: There was an error parsing the query. [ Token line number = 1,Token line offset = 20,Token in error = Table ]

This error happens at the line "cmd.ExecuteNonQuery()" after I click Done in my form. I am stumped. Does anyone see what I am doing wrong?

Thanks in advance.

hello !
i think there is prob with your query , you are using this one

INSERT INTO Sensor Table (Equipment ID, Location, Supervisor, Alarm Start, Alarm Stop) Values (@Id, @Loc, @Sup, @Astr, @Asto)

in above mentioned query if your table name is sensor then remove word table after sensor , and if your table name is sensor_table then use underscore .

Regards

This article has been dead for over six months. Start a new discussion instead.