0

plsss I need help!!

I have problem with my insert data into database
this is my code

Public Sub doSave()

        Dim con As New OleDb.OleDbConnection(My.Settings.SystemDataConnectionString)
        Dim cmd As New OleDb.OleDbCommand
        Dim adap As New OleDb.OleDbDataAdapter
        Dim ds As New DataSet
        Dim dr As OleDb.OleDbDataReader

        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\D_S_S - DATA\D_S_S\DSReS\SystemData.mdb;Persist Security Info=True")
        con.Open()
        cmd.CommandText = "SELECT * FROM tblVehicle WHERE VehiclePlat ='" & TxtPlat.Text & "'"
        cmd = New OleDbCommand(cmd.CommandText, con)
        dr = cmd.ExecuteReader()
        If Not dr.HasRows Then

            cmd.CommandText = " INSERT INTO tblVehicle (VehicleNo,vehicleModel,vehiclePlat,Type of License,TotalOfDriver) VALUES ('" + TxtNo.Text + "','" + TxtModel.Text + "','" + TxtPlat.Text + "','" + CmbLicense.Text + "','" + CmbDriver.Text + "')"
            cmd = New OleDbCommand(cmd.CommandText, con)
            cmd.Connection = con
            cmd.ExecuteNonQuery()
            MessageBox.Show("Data has been successfully saved.", "Data", MessageBoxButtons.OK, MessageBoxIcon.Information)


            adap = New OleDbDataAdapter("Select * from tblVehicle", con)
            adap.Fill(ds, "1")
            TblVehicleDataGridView1.DataSource = ds.Tables("1")
            Me.TblVehicleDataGridView1.Rows(Me.TblVehicleDataGridView1.RowCount - 2).Selected = True
            con.Close()
        Else
            MsgBox(" Vehicle record exist")

        End If

    End Sub

(ERROR) at cmd.ExecuteNonQuery()

= Syntax error in INSERT INTO statement.

3
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by Reverend Jim
0

Always use the parameters.

cmd=new SqlCommand()
cmd.CommandText = " INSERT INTO tblVehicle (VehicleNo,vehicleModel,vehiclePlat,[Type of License],TotalOfDriver) VALUES (@VehicleNo,@vehicleModel,@vehiclePlat,@TypeofLicense,@TotalOfDriver)"
cmd.Connection=con
cmd.Parameters.Add("@VehicleNo",SqlDbType.VarChar,20).Value=TxtNo.Text
cmd.Parameters.Add("@vehicleModel",SqlDbType.VarChar,20).Value=TxtModel.Text
cmd.Parameters.Add("@vehiclePlat",SqlDbType.VarChar,20).Value=TxtPlat.Text
cmd.Parameters.Add("@TypeofLicense",SqlDbType.VarChar,20).Value=CmbLicense.Text
cmd.Parameters.Add("@TotalOfDriver",SqlDbType.VarChar,20).Value=CmbDriver.Text

con.Open()
cmd.ExecuteNonQuery()
con.Close()
0

And never use field names that contain spaces. You have one field named "Type of License". You could use the spaces if you referenced the field by [Type of License] but embedding spaces is just bad form. Remove the spaces and you should be OK.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.