Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Inserting new records in a child table

Inserting new records in a child table

Scheduled Pinned Locked Moved Visual Basic
helpdatabasebusinesssales
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Psyfo
    wrote on last edited by
    #1

    Hi everyone I have the folowing code in the Business class in my program, but when i run it says that there's an error in the sql. Can you please help Public Function AddSale(ByVal dtDate As DateTime, ByVal bookcode As String, ByVal InvoiceNo As String, ByVal qty As Integer, ByVal subtot As Single, ByVal vat As Single, ByVal tot As Single) As String Dim sql As String Dim cmd As OleDb.OleDbCommand Dim result As String = "" Try sql = "INSERT INTO SALES (Dates, BookCode, InvoiceNumber, Quantity, SubTotal, VAT, Total) Values ( " + dtDate + " ,'" + bookcode + "','" + InvoiceNo + "', qty , subtot, vat, tot)" cmd = New OleDb.OleDbCommand(sql, con) cmd.Parameters.Add(New OleDb.OleDbParameter("@qty", qty)) cmd.Parameters.Add(New OleDb.OleDbParameter("@subtot", subtot)) cmd.Parameters.Add(New OleDb.OleDbParameter("@vat", vat)) cmd.Parameters.Add(New OleDb.OleDbParameter("@tot", tot)) cmd.Parameters.Add(New OleDb.OleDbParameter("@dtDate", dtDate)) If cmd.Connection.State = ConnectionState.Closed Then cmd.Connection.Open() End If cmd.CommandText = sql cmd.ExecuteNonQuery() result = "true" Catch ex As Exception result = ex.Message.ToString() End Try Return result End Function

    B 1 Reply Last reply
    0
    • P Psyfo

      Hi everyone I have the folowing code in the Business class in my program, but when i run it says that there's an error in the sql. Can you please help Public Function AddSale(ByVal dtDate As DateTime, ByVal bookcode As String, ByVal InvoiceNo As String, ByVal qty As Integer, ByVal subtot As Single, ByVal vat As Single, ByVal tot As Single) As String Dim sql As String Dim cmd As OleDb.OleDbCommand Dim result As String = "" Try sql = "INSERT INTO SALES (Dates, BookCode, InvoiceNumber, Quantity, SubTotal, VAT, Total) Values ( " + dtDate + " ,'" + bookcode + "','" + InvoiceNo + "', qty , subtot, vat, tot)" cmd = New OleDb.OleDbCommand(sql, con) cmd.Parameters.Add(New OleDb.OleDbParameter("@qty", qty)) cmd.Parameters.Add(New OleDb.OleDbParameter("@subtot", subtot)) cmd.Parameters.Add(New OleDb.OleDbParameter("@vat", vat)) cmd.Parameters.Add(New OleDb.OleDbParameter("@tot", tot)) cmd.Parameters.Add(New OleDb.OleDbParameter("@dtDate", dtDate)) If cmd.Connection.State = ConnectionState.Closed Then cmd.Connection.Open() End If cmd.CommandText = sql cmd.ExecuteNonQuery() result = "true" Catch ex As Exception result = ex.Message.ToString() End Try Return result End Function

      B Offline
      B Offline
      bigjokey
      wrote on last edited by
      #2

      When adding parameters, the parameter names in the SQL must match the parameters that you add: so your SQL should look like this: sql = "INSERT INTO SALES (Dates, BookCode, InvoiceNumber, Quantity, SubTotal, VAT, Total) Values ( " + dtDate + " ,'" + bookcode + "','" + InvoiceNo + "', @qty , @subtot, @vat, @tot)" You are also adding the date as a parameter "cmd.Parameters.Add(New OleDb.OleDbParameter("@dtDate", dtDate))" after you have already embedded it in the SQL. So you may want to change your SQL to look something like this: sql = "INSERT INTO SALES (Dates, BookCode, InvoiceNumber, Quantity, SubTotal, VAT, Total) Values ( @dtDate ,'" + bookcode + "','" + InvoiceNo + "', @qty , @subtot, @vat, @tot)" And given that you are using parameters, you may want to add the BookCode as a paramter aswell, for consistency. Hope this helps

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups