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
  1. Home
  2. General Programming
  3. Visual Basic
  4. error in this code

error in this code

Scheduled Pinned Locked Moved Visual Basic
databasehelpquestion
19 Posts 6 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.
  • M magedhv

    thanks for ur reply but it still gives the same error here what i wrote s = "insert into addcard" s = s & " (dte,op,nos,cost,coms,nii,npp )values " s = s & "(" & f1 & "," & f2 & "," & f3 & "," & f4 & "," & "," & f5 & "," & "," & f6 & "," & "," & f7 & "," & f8 & "')" cmd.CommandText = s h = cmd.ExecuteNonQuery() MsgBox("Saved succssefuly") plz give me ur feedback

    S Offline
    S Offline
    SHatchard
    wrote on last edited by
    #7

    Could you give an indication as to the error which is being returned from SQL when you try and execute the query?

    1 Reply Last reply
    0
    • M magedhv

      thanks for ur reply but it still gives the same error here what i wrote s = "insert into addcard" s = s & " (dte,op,nos,cost,coms,nii,npp )values " s = s & "(" & f1 & "," & f2 & "," & f3 & "," & f4 & "," & "," & f5 & "," & "," & f6 & "," & "," & f7 & "," & f8 & "')" cmd.CommandText = s h = cmd.ExecuteNonQuery() MsgBox("Saved succssefuly") plz give me ur feedback

      C Offline
      C Offline
      ChandraRam
      wrote on last edited by
      #8

      magedhv wrote:

      here what i wrote s = "insert into addcard" s = s & " (dte,op,nos,cost,coms,nii,npp )values " s = s & "(" & f1 & "," & f2 & "," & f3 & "," & f4 & "," & "," & f5 & "," & "," & f6 & "," & "," & f7 & "," & f8 & "')"

      Is this an exact copy from your code? If so, there is an extra ' at the end. Also, what is the type of field dte in your database? If it is a date, you cannot pass in a string, AFAIK. What is the error message you get, anyway?

      M 1 Reply Last reply
      0
      • M magedhv

        syntex error do u have another solution i have already use this code in another form and it works well (in my first topic ) i don't know what's wrong

        M Offline
        M Offline
        magedhv
        wrote on last edited by
        #9

        he gives me error when i tried to enter 2 values this is the error syntax error (missing operator) in query expression '7/2/2007Vodafone'.

        C 1 Reply Last reply
        0
        • C ChandraRam

          magedhv wrote:

          here what i wrote s = "insert into addcard" s = s & " (dte,op,nos,cost,coms,nii,npp )values " s = s & "(" & f1 & "," & f2 & "," & f3 & "," & f4 & "," & "," & f5 & "," & "," & f6 & "," & "," & f7 & "," & f8 & "')"

          Is this an exact copy from your code? If so, there is an extra ' at the end. Also, what is the type of field dte in your database? If it is a date, you cannot pass in a string, AFAIK. What is the error message you get, anyway?

          M Offline
          M Offline
          magedhv
          wrote on last edited by
          #10

          he gives me this error msg Syntax error in INSERT INTO statement. all of the fields in database are number except dte it's type (date/time) plz help me cause it drive me crazy

          C 1 Reply Last reply
          0
          • M magedhv

            he gives me this error msg Syntax error in INSERT INTO statement. all of the fields in database are number except dte it's type (date/time) plz help me cause it drive me crazy

            C Offline
            C Offline
            ChandraRam
            wrote on last edited by
            #11

            magedhv wrote:

            Syntax error in INSERT INTO statement. all of the fields in database are number except dte it's type (date/time)

            Ok - but did you check out the suggestions I made earlier? Date fields cannot be passed in as strings to Jet databases.

            1 Reply Last reply
            0
            • M magedhv

              he gives me error when i tried to enter 2 values this is the error syntax error (missing operator) in query expression '7/2/2007Vodafone'.

              C Offline
              C Offline
              ChandraRam
              wrote on last edited by
              #12

              magedhv wrote:

              syntax error (missing operator) in query expression '7/2/2007Vodafone'

              It looks like you dont have the two fields separated - shouldnt it be '7/2/2007','Vodafone'... Also, the first is a date field, IIRC date fields for Jet databases should be enclosed by # not quotes.

              M 1 Reply Last reply
              0
              • C ChandraRam

                magedhv wrote:

                syntax error (missing operator) in query expression '7/2/2007Vodafone'

                It looks like you dont have the two fields separated - shouldnt it be '7/2/2007','Vodafone'... Also, the first is a date field, IIRC date fields for Jet databases should be enclosed by # not quotes.

                M Offline
                M Offline
                magedhv
                wrote on last edited by
                #13

                plz write 4 me the code of the date to try thanks i appreciate your efforts

                C 1 Reply Last reply
                0
                • M magedhv

                  plz write 4 me the code of the date to try thanks i appreciate your efforts

                  C Offline
                  C Offline
                  ChandraRam
                  wrote on last edited by
                  #14

                  Just remember this... for text (string) variables, enclose data in quotes - like 'Vodafone' for number variables, just the value will do for date variables, enclose data with # - like #7/2/2007# and between each field, you should have a comma. Hope this helps.

                  M 1 Reply Last reply
                  0
                  • C ChandraRam

                    Just remember this... for text (string) variables, enclose data in quotes - like 'Vodafone' for number variables, just the value will do for date variables, enclose data with # - like #7/2/2007# and between each field, you should have a comma. Hope this helps.

                    M Offline
                    M Offline
                    magedhv
                    wrote on last edited by
                    #15

                    do u mean like that dim x as string x = "'" # DateTimePicker1.Value # "'" if that's what u mean the visual basic underline the last line (x = "'" # DateTimePicker1.Value # "'" ) plz try to write the whole code and note that the same code i use it with the same string in other form and it worked well

                    C 1 Reply Last reply
                    0
                    • M magedhv

                      do u mean like that dim x as string x = "'" # DateTimePicker1.Value # "'" if that's what u mean the visual basic underline the last line (x = "'" # DateTimePicker1.Value # "'" ) plz try to write the whole code and note that the same code i use it with the same string in other form and it worked well

                      C Offline
                      C Offline
                      ChandraRam
                      wrote on last edited by
                      #16

                      magedhv wrote:

                      x = "'" # DateTimePicker1.Value # "'"

                      Thats not what I mean... try this: x = "#" & DateTimePicker1.Value & "#"

                      1 Reply Last reply
                      0
                      • M magedhv

                        he tell me there is an error on the insert statement and iam sure from the names of the feilds coz i copy it from database so what's wrong??? i don't know where is the error plz anyone answer me thanks cmd.Connection.Close() If cn.State = ConnectionState.Closed Then If cn.State = ConnectionState.Closed Then cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ehab.mdb" cn.Open() End If cmd.Connection = cn cmd.CommandType = CommandType.Text End If Dim s As String Dim h As Integer s = 0 h = 0 Dim f1, f2, f3, f4, f5, f6, f7, f8 As String f1 = "'" & DateTimePicker1.Value & "'," f2 = "'" & ComboBox1.Text & "'," f3 = "'" & ComboBox2.Text & "'," f4 = "'" & TextBox1.Text & "'," f5 = "'" & TextBox2.Text & "'," f6 = "'" & TextBox3.Text & "'," f7 = "'" & TextBox4.Text & "'," f8 = "'" & Label10.Text & "'" s = "insert into sale " s = s & " (dte,op,value,nos,cost,coms,nii,npp )values " s = s & "(" & f1 & f2 & f3 & f4 & f5 & f6 & f7 & f8 & ")" cmd.CommandText = s h = cmd.ExecuteNonQuery() MsgBox("Saved succssefuly")

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #17

                        Ignore ALL the other replies you've gotten in this thread. They're all crap. Drop all this string concatenation garbage and do it the correct way with a parameterized query. See this article[^] by Colin Angus Mackay for more information as to what they are and why your shooting yourself in the foot doing it your way. You're actaully making is harder on yourself by building a string for your parameters.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007

                        M 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          Ignore ALL the other replies you've gotten in this thread. They're all crap. Drop all this string concatenation garbage and do it the correct way with a parameterized query. See this article[^] by Colin Angus Mackay for more information as to what they are and why your shooting yourself in the foot doing it your way. You're actaully making is harder on yourself by building a string for your parameters.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                               2006, 2007

                          M Offline
                          M Offline
                          magedhv
                          wrote on last edited by
                          #18

                          could u plz tell me what should i do because i read this topic but i didn't understand what to do so if u have any idea about what to do simply tell me and thanks i appreciate ur efforts

                          D 1 Reply Last reply
                          0
                          • M magedhv

                            could u plz tell me what should i do because i read this topic but i didn't understand what to do so if u have any idea about what to do simply tell me and thanks i appreciate ur efforts

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #19

                            I did "simply" tell you. You're building an SQL query by appending multiple strings together. This introduces the opportunity for a lot of bugs and security holes into your code. Read the article I gave you a link for and about half way down, it'll explain what a parameterized query is and how to put it together to solve most of your security and readability problems.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                 2006, 2007

                            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