Inserting into Access Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slider
    New Member
    • Feb 2007
    • 27

    Inserting into Access Database

    Hey all! need some help editing this script. The problem i keep running into is that i do not know how to change the insert into statement to insert the USER_ID and two other variables called path1 and path2. I keep getting syntax errors when i try to change it my self. Also the USER_ID which is coming from a cookie is not working well either. It tells me that its not declared when i put it into the insert into statement. Im using Windows XP with IIS 5.1. The page is a ASPX

    Thanks!

    Code:
    <%@ Page Language=VB Debug=true %>
    <% 
    Dim USER_ID
    USER_ID = request.cookies("USER_ID").value%>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OLEDB" %>
    <script runat=server>
    Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
        Dim DBConn as OleDbConnection
        Dim DBAdd As New OleDbCommand
        DBConn = New OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("DB/db1.mdb;"))
        DBAdd.CommandText = "Insert Into Employee (" _
            & "LastName, FirstName ) values (" _
            & "'" & Replace(txtLastName.Text, "'", "''") _
            & "', " _
            & "'" & Replace(txtFirstName.Text, "'", "''") _
            & "')"
        DBAdd.Connection = DBConn
        DBAdd.Connection.Open
        DBAdd.ExecuteNonQuery()
    End Sub
    </SCRIPT>
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by slider
    Hey all! need some help editing this script. The problem i keep running into is that i do not know how to change the insert into statement to insert the USER_ID and two other variables called path1 and path2. I keep getting syntax errors when i try to change it my self. Also the USER_ID which is coming from a cookie is not working well either. It tells me that its not declared when i put it into the insert into statement. Im using Windows XP with IIS 5.1. The page is a ASPX

    Thanks!

    Code:
    <%@ Page Language=VB Debug=true %>
    <% 
    Dim USER_ID
    USER_ID = request.cookies("USER_ID").value%>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OLEDB" %>
    <script runat=server>
    Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim DBConn as OleDbConnection
    Dim DBAdd As New OleDbCommand
    DBConn = New OleDbConnection( _
    "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
    & "DATA SOURCE=" _
    & Server.MapPath("DB/db1.mdb;"))
    DBAdd.CommandText = "Insert Into Employee (" _
    & "LastName, FirstName ) values (" _
    & "'" & Replace(txtLastName.Text, "'", "''") _
    & "', " _
    & "'" & Replace(txtFirstName.Text, "'", "''") _
    & "')"
    DBAdd.Connection = DBConn
    DBAdd.Connection.Open
    DBAdd.ExecuteNonQuery()
    End Sub
    </SCRIPT>
    give your full sql query which you are using

    Comment

    • radcaesar
      Recognized Expert Contributor
      • Sep 2006
      • 759

      #3
      Hey what is the exact error message u got ?

      Comment

      • slider
        New Member
        • Feb 2007
        • 27

        #4
        Originally posted by dip_developer
        give your full sql query which you are using
        umm that is the full SQL query....not unless you want the rest of the HTML page

        Originally posted by radcaesar
        Hey what is the exact error message u got ?
        i get just a error message saying i have caused a syntax error. It comes up becuase i have no clue what so ever.

        The script works when i do not change it. It will get a first name and a last name out of two text fields and write them to the database. I need this script to write a USER_ID which is extracted from a cookie and to also write two variables called IMG and THUMB into the fields in the database called USER_ID, IMG, and THUMB

        hope this makes it more understandable

        Comment

        • radcaesar
          Recognized Expert Contributor
          • Sep 2006
          • 759

          #5
          U had unnecessarily use the concat operators in many places.

          Just Have a break point in that line and see the returned query string which was returned.

          There you will see your fault.

          Remove the unnecessary & operators.
          Check the Replace() mthod u use as string.

          :)

          Comment

          • radcaesar
            Recognized Expert Contributor
            • Sep 2006
            • 759

            #6
            Proceed it like this,

            "Insert Into Employee (LastName, FirstName ) values (Replace(" &txtLastName.Te xt& ","

            Comment

            • slider
              New Member
              • Feb 2007
              • 27

              #7
              Hey! Thanks a lot it all works now!
              However my only problem now is the USER_ID. I can insert other cookie values into the database such as nicknames but not the USER_ID which is a numeric value such as 123.

              When i run the page i get this error

              System.NullRefe renceException: Object reference not set to an instance of an object.

              EG
              Code:
              dim userID 
              userID = request.cookies("USER_ID").value
              Any help would be great

              Comment

              Working...