Storing data to Access from Visual Studio VB ASP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NiallP17
    New Member
    • Oct 2013
    • 18

    Storing data to Access from Visual Studio VB ASP

    I am trying to store three values that I take from my web form and place them into a database that I have.

    At the minute when I try this, the (UserID, Title, Description) line doesn't accept and the Title and Description are underlined in blue. I have tried putting in & instead of the comma but then it takes it all as one long string.

    I have tried so many ways to try and get it to place the right data into the right space in my database and I cannot seem to work out what I'm doing wrong.

    Any help would be greatly appreciated.

    Many thanks


    Code:
        Function SubmitRequest() As Boolean
    
            Dim myConn As OleDbConnection
            Dim myCmd As OleDbCommand
            Dim myDr As OleDbDataReader
            Dim RequestTable As New DataTable
            Dim UserID As Integer = CType(Session.Item("UserID"), String)
            Dim Title As String = txtRequestTitle.Text
            Dim Description As String = txtProblemDescription.Text
    
    
    
    
            myConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Helpdesk\Test\Database.accdb;Persist Security Info=True")
            myConn.Open()
            'myCmd = New OleDbCommand("select ID, Title, ProblemDescription from Request where StaffID=" & UserID & "", myConn)
            'myCmd = New OleDbCommand("INSERT StaffID, Title, ProblemDescription) VALUES ( & UserID & Title & Description" & "", myConn)
            'myCmd = New OleDbCommand("INSERT Request StaffID, Title, ProblemDescription) VALUES " & '"UserID, Title, Description"'"), myConn)
            'myCmd = New OleDbCommand("INSERT INTO Request StaffID, Title, ProblemDescription) VALUES (@UserID, @Title, @Description)", myConn)
    
            myCmd = New OleDbCommand("INSERT INTO Request StaffID, Title, ProblemDescription) VALUES "(UserID, Title, Description), myConn)
            'myDr = myCmd.ExecuteReader
            'RequestTable.Load(myDr)
    
    
    
            Return True
    
    
        End Function
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The correct format for an insert query is:
    Code:
    INSERT INTO tableName (comma delimited field list)
    VALUES (comma delimited value list)

    Comment

    • NiallP17
      New Member
      • Oct 2013
      • 18

      #3
      Code:
      myCmd = New OleDbCommand("INSERT INTO Request (StaffID, Title, Description) VALUES "(UserID, Title, Description), myConn)
      Do you mean like that? Because Title, Description are underlined blue as 'Too many arguments to 'Public ReadOnly Default Property Chars(index as integer) As Char'.
      Last edited by NiallP17; Oct 20 '13, 09:49 PM. Reason: Typo

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        You didn't append your value list correctly.

        Comment

        • NiallP17
          New Member
          • Oct 2013
          • 18

          #5
          So I am listing them wrong? How am I suppose to list them?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            The commas and parentheses need to be a part of the string. The variables you want to append need to be outside of the string.

            Comment

            • NiallP17
              New Member
              • Oct 2013
              • 18

              #7
              I'm really sorry but I don't understand what that means

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                What I mean is this:
                Code:
                myCmd = New OleDbCommand("INSERT INTO Request (StaffID, Title, Description) VALUES (" & UserID & ",'" & Title & "','" & Description & "')", myConn)

                Comment

                • NiallP17
                  New Member
                  • Oct 2013
                  • 18

                  #9
                  That line of code worked but now I am getting an error on the line
                  Code:
                  myDr = myCmd.ExecuteReader
                  It is saying 'Syntax Error (missing operator) in query expression 'the wifi is not working')'.

                  The expression is what i am trying to store as the description in my table.

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    Please output the full SQL query it is attempting to run. The most likely cause is that you have quotes in your string which you need to escape.

                    Comment

                    • NiallP17
                      New Member
                      • Oct 2013
                      • 18

                      #11
                      Code:
                      myCmd = New OleDbCommand("INSERT INTO Request (StaffID, Title, Description) VALUES (" & UserID & ",'" & Title & "','" & Description & "')", myConn)
                      That is the SQL I am using

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        That's your code. I want to see the SQL string that is being passed to the server.

                        Comment

                        • NiallP17
                          New Member
                          • Oct 2013
                          • 18

                          #13
                          It's the same code that is posted in the first comment.

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            I know. I want to see the SQL that is passed to the server, not the code. I don't want to see the code because that doesn't tell me what is passed to the server.

                            Comment

                            • NiallP17
                              New Member
                              • Oct 2013
                              • 18

                              #15
                              Isn't the SQL in the code?

                              Comment

                              Working...