I am trying to insert data into an MS Access DB through a VB.net Windows Forms App
when executing this code
Dim con As New OleDbConnection(CS)
Dim cmd As New OleDbCommand("INSERT INTO Ports ([PortNumber] , [DistributionBoardLocation] , [DistributionBoardSubLocation] , [PortLocation] , [PortSubLocation] , [PortDevice] , [POE]) VALUES('" & nudPortNumber.Value & "', '" & txtDBLocation.Text & "', '" & txtDBSubLocation.Text & "' , '" & txtPortLocation.Text & "' , '" & txtPortSubLocation.Text & "' , '" & txtPortDevice.Text & "' , '" & chPOE.Checked & "'", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MsgBox("New Port Has Been Created")
i get this error message -
System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement
Please Help
Thanks,
Jacob
You insert every value as text which probably is not the case. Adjust that, for example for the port number:
.. VALUES (" & nudPortNumber.Value & ", ..
Related
PLease I need help whenever I register my form to database, it says registration successful but the last table (the TRANSACTION table) data information from my form does not input the info in my database only the table FORM and STUDENT have data from my form. Is there something wrong in my TRANSACTION code? or in database?
PLEASE HELP :((
sql = "INSERT INTO FORM VALUES ('" & txtformnum.Text & "' , '" & bcboRequest.Text & "' , '" & txtTotal.Text & "')"
da = New OleDb.OleDbDataAdapter(sql, con) '"
da.Fill(ds, "FORM")
sql = "INSERT INTO STUDENTS VALUES ('" & txtstudnum.Text & "','" & txtSurname.Text & "','" & txtGName.Text & "', '" & txtMName.Text & "', '" & txtAddress.Text & "', '" & status & "' , '" & txtYr.Text & "' , '" & cbostype.Text & "' , '" & chkClearance.Text & "', '" & txtCourse_Track.Text & "' , '" & txtCNumber.Text & "' , '" & dot.Value & "' , '" & dotdue.Value & "')"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "STUDENTS")
Dim sqlquery As String = "INSERT INTO TRANSACTION (Transaction_num,Stud_num,Form_num,Total Fee)" + "VALUES (" & txttransactionno.Text & ",'" & txtstudnum.Text & "'," & txtformnum.Text & "," & txtTotal.Text & ");"
Dim sqlcommand As New OleDb.OleDbCommand(sqlquery)
I don't know what database you're using, but if you're using MySQL or MS SQL Server then the keyword TRANSACTION is reserved and must be escaped to work within your statement.
If using MySQL, try changing your statement to INSERT INTO "TRANSACTION"
If using SQL Server, change your statement to INSERT INTO [TRANSACTION]
If you're not using either of those, post what database system you're using and I'll post the proper escape syntax.
Try changing this line
Dim sqlquery As String = "INSERT INTO TRANSACTION (Transaction_num,Stud_num,Form_num,Total Fee)" + "VALUES (" & txttransactionno.Text & ",'" & txtstudnum.Text & "'," & txtformnum.Text & "," & txtTotal.Text & ");"
To
Dim sqlquery As String = "INSERT INTO TRANSACTION ([Transaction_num],[Stud_num],[Form_num],[Total Fee]) " + "VALUES ('" & txttransactionno.Text & "','" & txtstudnum.Text & "','" & txtformnum.Text & "','" & txtTotal.Text & "');"
This should work fine.
Add the following to your code:
sqlcommand.ExecuteNonQuery()
Dim cmd As New OleDbCommand
OpenConnection()
cmd.Connection = con
cmd.CommandText = "INSERT INTO [StudentInfo] ([Stud_Num], [Stud_FName], [Stud_LName], [Stud_ContactNum], [Stud_Email], [Stud_Address], [Stud_Username], [Stud_Password], [Stud_ConfirmPassword]) VALUES (" & _
txtStudNum.Text & ", '" & txtFname.Text & "', '" & txtLname.Text & "', '" & _
txtNum.Text & "', '" & txtEmail.Text & "', '" & txtAddress.Text & "' , '" & txtUsername.Text & "', '" & txtPassword.Text & "', '" & txtConPassword.Text & "')"
cmd.ExecuteNonQuery()
The error was the cmd.ExecuteNonQuery() I'm still a student and a noob in using vb.net so please could you tell me what I did wrong ?
Wondering if someone can help me with this please. I'm getting the following error message when adding data to an access database.I know it's a time issue function but I don't know how to fix it so I can add data to the database in the required format. One or more values are prohibited by the validation rule 'Time()' set for 'tblmph'. Enter a value that the expression for this field can accept.
Public Sub Add_Data()
con.Open()
Dim rs As New OleDb.OleDbCommand("Insert into tblmph(ID,ThisDate,TimeStart,TimeFinish,Notes) " _
& "values ('" & TextBox1.Text & "' , '" & TextBox2.Text & "' , '" & TextBox3.Text & "' , '" _
& TextBox4.Text & "', '" & TextBox5.Text & "')", con)
rs.ExecuteNonQuery()
con.Close()
Display_Data()
End Sub
The columns are formatted as follows
ID = Auto Number-Long Integer
ThisDate = Short Date
TimeStart = Medium Time, Default Value = Time()
TimeFinish = Medium Time, Default Value = Time()
Notes = Memo
Do not insert the value of ID. If it is truly an auto-number, Access will automatically assign a value to the newly inserted row. Trying to insert a value into the field will cause an error and prevent the insert from working.
Change your Dim statement to:
Dim rs As New OleDb.OleDbCommand( "Insert into tblmph( ThisDate, TimeStart, TimeFinish, Notes ) values ( '" & TextBox2.Text & "' , '" & TextBox3.Text & "' , '" & TextBox4.Text & "', '" & TextBox5.Text & "' ) ", con)
When running the program i get an error saying 'Data type mismatch in criteria expression.' and the line cmd.ExecuteNonQuery() is highlighted. In my database the datatype for 'ID' is AutoNumber and the datatype for 'Calories Burned' is decimal and everything else is text. I don't know if it is do with fact that when i input data into the text boxes its classed as a string. but if someone could help i would appreciate it a lot.
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim cmd As New OleDb.OleDbCommand
' add data to table '
If Not cnn.State = ConnectionState.Open Then
' open connection '
cnn.Open()
End If
cmd.Connection = cnn
If Me.txtID.Tag & "" = "" Then
cmd.CommandText = "INSERT INTO [Training log] ([ID], [Runner Name], [Running Average Speed], [Cyclying Average Speed], [Swimming style] , [Calories Burned]) VALUES ('" & Me.txtID.Text & "' , '" & Me.txtRunnerName.Text & "' , '" & Me.txtRunSpeed.Text & "' , '" & Me.txtCycleSpeed.Text & "', '" & Me.txtSwimStyle.Text & "', '" & Me.txtCaloriesBurned.Text & "')"
cmd.ExecuteNonQuery()
Else
cmd.CommandText = "UPDATE [Training log] SET ID=" & Me.txtID.Text & ", [Runner Name]='" & Me.txtRunnerName.Text & "', [Running Average Speed]='" & txtRunSpeed.Text & "', [Cyclyin Average Speed]='" & txtCycleSpeed.Text & "', [Swimming style]='" & txtSwimStyle.Text & "', [Calories Burned]='" & txtCaloriesBurned.Text & "' WHERE ID='" & txtRunnerName.Tag & "' "
cmd.ExecuteNonQuery()
End If
You may need to remove the single quotes around your non-string values.
cmd.CommandText = "INSERT INTO [Training log] ([ID], [Runner Name], [Running Average Speed], [Cyclying Average Speed], [Swimming style] , [Calories Burned]) VALUES (" & Me.txtID.Text & " , '" & Me.txtRunnerName.Text & "' , '" & Me.txtRunSpeed.Text & "' , '" & Me.txtCycleSpeed.Text & "', '" & Me.txtSwimStyle.Text & "', " & Me.txtCaloriesBurned.Text & ")"
I am using Visual Studio 2008 and have connected a database correctly as I have done a login that works fine, although when I try to insert information submitted in the text boxes a different table, it doesn't enter after I end program to check it still has no data in. Any ideas?
Dim con As SqlCeConnection = New SqlCeConnection("Data Source=NESdb.sdf")
Dim myDA As SqlCeDataAdapter
Dim myDataSet As DataSet
Dim dt As New DataTable()
'Connect to database'
con.Open()
'Attempt to retrieve data'
Try ' Select username and password that match'
Dim cmd As SqlCeDataAdapter = New SqlCeDataAdapter("INSERT INTO ScrapVehicles(Fname, Lname, Add1, Add2, Town, PostCode, Telephone, Mob, Email, VehicleType, RegNo, Year, Make, Model, V5, Collected, CollectionDate)" + "VALUES('" & txtFname.Text & "', '" & txtLname.Text & "', '" & txtAdd1.Text & "', '" & txtAdd2.Text & "', '" & txtTown.Text & "', '" & txtPostCode.Text & "', '" & txtTelephone.Text & "', '" & txtMob.Text & "', '" & txtEmail.Text & "', '" & comboVehicleType.Text & "', '" & txtReg.Text & "', '" & comboYear.Text & "', '" & comboMake.Text & "', '" & txtModel.Text & "', '" & chkV5.Text & "', '" & chkCollected.Text & "', '" & dtpWhen.Text & "')", con)
'Catch errors'
Catch ex As Exception
End Try
'Close connection to database'
If con.State <> ConnectionState.Closed Then
con.Close()
End If
You're building up the cmd object, but you don't execute it.
Suggest ditch the Adapter when inserting. Try this instead:
/*snipped values for brevity.*/
Dim insertSql As String = "INSERT INTO ScrapVehicles(Fname, Lname, Add1, Add2, Town) VALUES(#FName, #LName, #Add1, #Add2, #Town)"
conn.Open()
Dim cmd As New SqlCeCommand(insertSql, conn)
cmd.Parameters.Add(New SqlCeParameter("#FName", txtFirstName.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("#LName", txtLastName.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("#Add1", txtAdd1.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("#Add2", txtAdd2.Text.Trim()))
cmd.ExecuteNonQuery()
conn.Close()
You are missing a bracket at the end
& dtpWhen.Text & "'", con)
should be
& dtpWhen.Text & "')", con)
Instead of using a SqlCeDataAdapter, use a SqlCeCommand object. And after creating it, actually use it (call ExecuteNonQuery on it). And remove the Try, Catch Ex as Exception and End Try lines so that, if an error occurs, you'll actually see it.
That's what I can see from 30 seconds of looking.
Edit
You should also look at using parameters rather than concatenating the INSERT statement together.