5 Replies - 2445 Views - Last Post: 13 April 2010 - 05:42 AM Rate Topic: -----

#1 childlucy00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-February 10

Help with INSERT INTO statement in VB.NET using SQL

Posted 13 April 2010 - 01:48 AM

I've got this code. But I keep getting an INSERT INTO error when I try to insert a new field into the database


Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim recordsadded As Integer
Dim strSQLString1, strSQLString2, strSQLString As String
strSQLString1 = "INSERT INTO tblParts (PartName, PartPrice, Part Supplier) "
strSQLString2 = "VALUES ('" & txtPartName.Text & "','" & txtPartPrice.Text & "','" & txtPartSupplier.Text & "')"
strSQLString = strSQLString1 + strSQLString2
MessageBox.Show(strSQLString)
Dim NewPartTableAdaptor As New DataTable
Try
Dim dbCommand As New OleDbCommand
dbCommand.Connection = LogInForm.dbConnection
dbCommand.CommandType = CommandType.Text
dbCommand.CommandText = strSQLString
recordsadded = dbCommand.ExecuteNonQuery()
MessageBox.Show(recordsadded)
Catch ex As Exception
MessageBox.Show(ex.Message & "-" & ex.Source)
End Try
End Sub


My 3 datatypes are text values, and I have anoter form adding entries to my database in another table, which are all text values.

Any ideas?


Lucy

Is This A Good Question/Topic? 0
  • +

Replies To: Help with INSERT INTO statement in VB.NET using SQL

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Help with INSERT INTO statement in VB.NET using SQL

Posted 13 April 2010 - 01:57 AM

what is the error.

This post has been edited by NoBrain: 13 April 2010 - 01:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 childlucy00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-February 10

Re: Help with INSERT INTO statement in VB.NET using SQL

Posted 13 April 2010 - 02:08 AM

Syntax Error in INSERT INTO statement.-Microsoft JET Database Engine

Syntax Error in INSERT INTO statement.-Microsoft JET Database Engine
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Help with INSERT INTO statement in VB.NET using SQL

Posted 13 April 2010 - 02:47 AM

try without using ' in your values section and check if there is no reserved word in SQL that you use in your table Supplier maybe one
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1532
  • View blog
  • Posts: 4,908
  • Joined: 25-September 09

Re: Help with INSERT INTO statement in VB.NET using SQL

Posted 13 April 2010 - 05:15 AM

Enclose Part Supplier inside brackets,
INSERT INTO tblParts (PartName, PartPrice, [Part Supplier])

Spaces aren't allowed without enclosing them in brackets.
Was This Post Helpful? 0
  • +
  • -

#6 JoshD  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 111
  • Joined: 22-March 10

Re: Help with INSERT INTO statement in VB.NET using SQL

Posted 13 April 2010 - 05:42 AM

If your Column Name contains a space it must be enclosed in square brackets (PartName, PartPrice, [Part Supplier])

Can i ask though. Why are you doing it in 2 parts (or 3 parts).

You could just

strSQLString = "INSERT INTO tblParts (PartName, PartPrice, [Part Supplier]) VALUES ('" & txtPartName.Text & "','" & txtPartPrice.Text & "','" & txtPartSupplier.Text & "')"


Also is this a ms access database or another type. here is an example if you were doing ms access with oledb.

            dataAdapter.InsertCommand = New OleDbCommand("INSERT INTO Project (PartName, PartPrice, [Part Supplier]) VALUES (@Name, @Price, @Supplier)")
            dataAdapter.InsertCommand.Connection = connectionString
            dataAdapter.InsertCommand.Parameters.AddWithValue("@Name", txtPartName.Text)
            dataAdapter.InsertCommand.Parameters.AddWithValue("@Price", txtPartPrice.Text)
            dataAdapter.InsertCommand.Parameters.AddWithValue("@Supplier", txtPartSupplier.Text)


This post has been edited by JoshD: 13 April 2010 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1