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.

Recommended Answers

All 2 Replies

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()

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.