SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ASP: Where's the Error?

    Alright, I'm a bit new to ASP...I've got the syntax down, but can't for the life of me figure out what's wrong here, or why I'm getting a 500 error, as opposed to a parse error, which usually gives you some clue as to what's wrong. Here's my code:

    Code:
    <% Option Explicit
       Dim conn, connection, submit, query, query_text
       ' ---------------------------------------------------
       submit = Request.Form("submit")
       Set conn = Server.CreateObject("ADODB.Connection")
       connection = "DRIVER={Microsoft Access Driver (*.mdb)};" & _ 
                    "DBQ=" & Server.MapPath("\genevarpc\db\articles.mdb") & ";"
       conn.Open(connection)
    
       ' ---------------------------------------------------
       If submit = False Then %>
       <form action="add.asp" method="post">
          Title: <input type="text" name="title" size="50"><p>
          Body of Article:<br>
          <textarea name="body" rows="20" cols="50" wrap></textarea><p>
          <input type="submit" name="submit">
       </form> %>
       Else
       ' ---------------------------------------------------
          query_text = "INSERT INTO articles SET title='" & title & "', body='" & body & "'" 
          Set query = conn.Execute(query_text)
          Set query = nothing
       End If
       ' ---------------------------------------------------
       conn.Close
       Set conn = nothing;
    %>
    Any help is appreciated.

  2. #2
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A couple of things that may be causing the problem.

    1) You've got the Option Explicit statement but you haven't declared the variables title and body, but you should be getting a parse error on this.

    2) I don't think that 'If submit = False Then' statement will ever evaluate as true because the submit variable will be in string format. Try using a different value for the submit input and test the submit for the string value rather than as a boolean.

    I often just use a hidden flag variable, with a value 2, which I can then check against.

    strFlag = Request.Form("flag") & ""
    If strFlag = "2" Then
    'form submitted
    End If


    One other thing, you don't need to assign the recordset resulting from the execute method. If you just leave off the 'Set query = ' the RS will be discarded. If you do this you may as well use the adExecuteNoRecords parameter to save the db from creating the RS in the first place.

    Const adExecuteNoRecords = 128
    conn.execute (query_text, , adExecuteNoRecords)

    This last point shouldn't cause any problems but is just a performance issue.

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so, if I understand you correctly, those two lines of code should work just fine? And thanks for the tips...I'll correct them, though I do agree that I should probably still be getting a parse error. What kind of things would cause a 500 error?

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A 500 error is a server error. I've seen it come up before when there's a problem with the script. It's a bit of a crappy error as it can cover a multitude of sins.

    I've spotted a couple of other things.

    Make sure you've put the Option Explicit statement before any other script, including html, but not before any directives.

    Also, the '</form> %>' should be '</form> <%'.
    Last edited by shane; Jul 6, 2001 at 15:05.

  5. #5
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've done some checking and the statement:

    If submit = False Then

    will evalute to True if the Form collection doesn't contain a 'submit' item, in which case 'submit' will be Empty which evaluates to False.

    Even if submit is initialized to some value it'll be reset to Empty, so using your method does appear to be ok. Apologises for the duff advice.

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No apology needed. I figured that was the case, but it makes no difference to me either way. Thanks for the bit of opening ASP code by the closing <form> tag -- how could I have missed that? Still got the same error, though. I mean, I do know what a 500 error is and all, I just dno't understand why I'm getting it.

    Here's my current code -- anyone see anything here?

    Code:
    <% Option Explicit
       Dim conn, connection, switch, title, body, query_text
       ' ---------------------------------------------------
       switch = Request.Form("switch")
       title  = Request.Form("title")
       body   = Request.Form("body")
    
       Set conn   = Server.CreateObject("ADODB.Connection")
       connection = "DRIVER={Microsoft Access Driver (*.mdb)};" & _ 
                    "DBQ=" & Server.MapPath("\genevarpc\db\articles.mdb") & ";"
       conn.Open(connection)
    
       ' ---------------------------------------------------
       If switch = "submit" Then %>
       <form action="add.asp" method="post">
          <input type="hidden" name="switch" value="submit">
          Title: <input type="text" name="title" size="50"><p>
          Body of Article:<br>
          <textarea name="body" rows="20" cols="50" wrap></textarea><p>
          <input type="submit" name="submit">
       </form> <%
       Else
       ' ---------------------------------------------------
          query_text = "INSERT INTO articles SET title='" & title & "', body='" & body & "'" 
          Const adExecuteNoRecords = 128 
          conn.execute (query_text, , adExecuteNoRecords) 
        End If
       ' ---------------------------------------------------
       conn.Close
       Set conn = nothing;
    %>
    Last edited by TWTCommish; Jul 6, 2001 at 16:28.

  7. #7
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    query_text = "INSERT INTO articles SET title='" & title & "', body='" & body & "'"
    Should the above not be the following?
    Code:
    query_text = "INSERT INTO articles (title,body) VALUES('" & title & "', '" & body & "')"

  8. #8
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think so...I know that, at least when using MySQL, both ways work just fine. Even so, would that cause a 500 error, or is this free host I'm using going nuts?

  9. #9
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think the following is ANSI SQL and I'm pretty sure it won't work with Access.

    INSERT INTO <table> SET <field> = <value>

    I remember another SPF member getting 500 errors, when using a free host, when they had some script errors. I think that some hosts may configure their servers to raise this error instead the error info from the script.

  10. #10
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dunno if I'm using Access or SQL Server...probably Access, though. The thing is, wouldn't the error only occur if the form had been submitted? I don't think it would pick up a faulty SQL query in a page even if it hasn't been made yet.

  11. #11
    SitePoint Guru
    Join Date
    Dec 2000
    Location
    Karachi, Pakistan
    Posts
    913
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not Personal Web Server to check out the ASP Pages you create ?

    Well, I don't like to be after the host. Well, what else can you expect from a Hosting Company which hosts you for free on windows nt, 2000 server ?


    I again recommend installing PWS.


    Regards,
    Omair Haroon
    36Host.com - $36/year web hosting [affiliates earn 30%]
    * Affordable Small Business Web Hosting since 2003! *
    "500mb space, 10gb bandwith, 50 pop/ftp accounts, php,
    mysql, pre-installed php scripts, 24/7 support & more...."



  12. #12
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've used PWS, but It eats up some of my system resources, so I like to avoid it when possible. Anyway, the free host probably doesn't have much to do with it, because it's running other ASP pages that connect to the same DB without any problem. Anyway, I'm trying to get this done and up on the web soon...so I'd rather just correct this one bug and look into the details later, if it's possible.

  13. #13
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A stumper indeed. I will scan through more thoroughly and try to get back to you.

    Suggestions, meanwhile: Why don't you try adding records the "ASP way"? Instead of your insert SQL statement, try this on for size:
    Code:
    <%dim RS
    RS = Server.CreateObject("ADODB.Recordset")
    RS.Open "articles", conn, adOpenKeyset, adLockOptimistic
    dim varFields
    dim varValues
    varFields = Array("Title", "Body")
    varValues = Array(Title, Body)
    RS.AddNew varFields, varValues
    RS.Update
    'At this point, you've added the record. You can close the recordset/connection. I've included this.
    RS.Close
    Conn.Close
    Set RS = Nothing
    Set Conn = Nothing
    %>
    And there you have it. I hope it works, somehow. If not, it's still my preferred, logical way to do things. You can use the same RS object to edit / delete records, too. It fits the flow of my mind better.

    Hope this helps.

    'Till next time..
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)

  14. #14
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,264
    Mentioned
    115 Post(s)
    Tagged
    1 Thread(s)
    OK. A couple things:

    1) Are you using IE? If so, make sure the friendly HTTP error messages are turned OFF. This will tell you exactly which line is giving you the error

    2) The insert statement IS incorrect. The format for almost all versions of SQL I've ever seen is INSERT (field names) VALUE (values)

    Hopefully this will get you on the right track (especially point #1)
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  15. #15
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My mistake on #2 -- I'd always used that method for MySQL, and figured all would be okay. Changing my IE settings did in fact help. Here is a more detailed, helpful error:

    conn.execute (query_text, , adExecuteNoRecords)
    -----------------------------------------------^


    Any idea what's up shane?

  16. #16
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure the number value that you defined is the correct one?

    Also, try using it as a constant.

    Other than that, I respectfully bow out. I'm in over my head.
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)

  17. #17
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea. Dunno what adExecuteNoRecords does or anything. Anyway, I got it working by going back to the old, less-efficient query code...but this is going to be used very rarely, so it makes no difference to me. If someone still wants to educate me here anyway, I'm open to the idea.

  18. #18
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Argh -- another problem! I'm holding a big block of text in the DB, and it's only showing the first few sentences. Do I need to specify a larger datatype in my DB, or something like that?

  19. #19
    SitePoint Guru
    Join Date
    Dec 2000
    Location
    Karachi, Pakistan
    Posts
    913
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which Database are you using ?

    If you are using Access, then change the Article field to Memo..
    Because the text field only allows 255 fields.. no more..


    Regards,
    Omair Haroon
    36Host.com - $36/year web hosting [affiliates earn 30%]
    * Affordable Small Business Web Hosting since 2003! *
    "500mb space, 10gb bandwith, 50 pop/ftp accounts, php,
    mysql, pre-installed php scripts, 24/7 support & more...."



  20. #20
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thanks very much Omair! Works like a charm. Hate to ask another question, but here goes: is there an ASP function equivalent to PHP's nl2br(), to automatically preserve linebreaks?

  21. #21
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by TWTCommish
    Here is a more detailed, helpful error:

    conn.execute (query_text, , adExecuteNoRecords)
    -----------------------------------------------^

    Any idea what's up shane?
    Bit of a guess, but it may be to do with either the version of Access db or version of odbc driver. I've used the same method a lot in both SQL Server and Access 2000 without any problems.

    If you leave off the adExecuteNoRecords parameter option you should still be able to run the update, as below, without assigning the RS to a variable, so saving some overhead.

    conn.execute(query_text)


    Are you sick of my duff advice yet?

    The code below will probably do the same as the PHP funtion nl2br().

    strText = Replace(strText, vbCrLf, "<br />")

    The vbCrLf is the VBS built in constant for carriage return + linefeed.
    Last edited by shane; Jul 9, 2001 at 14:16.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •