Homework help for ASP

Discussion in 'Software' started by mars4bb, Apr 1, 2004.

  1. mars4bb

    mars4bb Private E-2

    Can someone please help with this work for my asp class.

    [font=Verdana, Arial, Helvetica, sans-serif]Create one ASP page -- you can duplicate the ConnPol.asp page used in Chapter 12 above. This will connect to the politics.mdb, politicians table. This should should start with the first record and display all fields in the first record. Use the MoveNext method to display each successive record. Also, add the MovePrevious method to show the previous record. Also, add buttons for both the MoveFirst and the MoveLast methods. The final page should be able to scroll both forwards and backwards one record (row) at a time and be able to take the user back to the first record or to the last record.[/font]

    Can someone help with the code part of this?
     
  2. Kodo

    Kodo SNATCHSQUATCH

    I can HELP you, but I'm not going to do it for you.. if you agree, we can begin..
     
  3. mars4bb

    mars4bb Private E-2

    thx

    I would love any HELP, I can get... no one in my class can get this.
     
  4. Kodo

    Kodo SNATCHSQUATCH

    It's a lot easier than it looks.. I am heading into work right now. I have a few things to start when I get there and will post back with your first set of "clues" if you will..
     
  5. Kodo

    Kodo SNATCHSQUATCH

    ok dude...really sorry it took so long. What a day!

    Ok. So you have to use a recordset for your assignment. In reality that's not necessary but just so you stick to the rules, we'll do that.

    First thing you need to do is create a recordset. Build a query and execute it.
    We'll assume this part has been done.

    so you test for eof.

    PHP:
     If RS.EOF then 
    'do nothing
    response.write "No Records To Display"
    else
     
    '
    build your page
     
     
     
    end 
    if
    you want to make sure that you have data in there otherwise you're wasting time and you'll get a bunch of errors.

    Now stop for a second and we'll back up to a very important part of this. THE CURSOR TYPE.. we need to figure out what cursor type we need to use so we can scroll forwards and backwards.. so let's do that now..

    We have 4 cursor types.. you pick which one you think we should use. The answer is obvious:

    adOpenForwardOnly: Default type that moves only one direction (forward) and is not scrollable but is the fastest cursor type.

    adOpenStatic: similar to forward only but is scrollable so you can move back to previoius records as well as moving forwards

    adOpenDynamic: Fully dynamic RS that lets you see ammendments and deletions and is fully scrollable.

    adOpenKeySet: similar to Dynamic but you can't see additions or deletions to records.


    There is actually 3 here that you can use. All will allow forward and reverse movement. But if you don't need to modify data in your recordset then you would use which cursor type?
    Make your choice now.
     
  6. mars4bb

    mars4bb Private E-2

    answer....

    We really appreciate you taking the time to help us!!!

    We believe that we'll need to use the adOpenStatic cursor type....
     
  7. Kodo

    Kodo SNATCHSQUATCH

    ok, in lieu of saving some time here I'll give you the answer to the next question.

    What lock type should we use?
    A: Since we're not updating anything we should use the default adLockReadOnly

    so we should now have something that looks like this


    PHP:
    Dim RSSQL
    set rs
    =server.createobject("adodb.recordset")
    SQL="Select * from users"
    rs.open sql,conn,adOpenStatic,adLockReadOnly
    if rs.eof then
             response
    .write "no records"
    else
      
    'do code
    end if

    Now we want to first scroll through all the records so that means we have to start at the first record and move to the last. We need a loop! Lets use the while ...wend loop

    Right, that's easy enough. So we place our while loop in our 'do code section

    PHP:
     Dim RSSQL
    set rs
    =server.createobject("adodb.recordset")
    SQL="Select * from users"
    rs.open sql,conn,adOpenStatic,adLockReadOnly
    if rs.eof then
    response
    .write "no records"
    else
              while 
    not rs.eof
              
    'recordset movement here

               wend
      
    end if

     
    Take a look at the above code sample. Take a shot at putting what you think is needed there. I'll give you a hint. In my sql query, I am pulling at least the firstname and lastname fields from the database. so you're going to response.write them out, but now you must implement how to move to the next record.. so go ahead and place your response.write code in there and your best guess as to where to put rs.movenext

    Remember, the variable RS is nothing special. It is something I chose to use to be something meaningful (Record Set). It could have been "boogers" for all we care.
     
  8. mars4bb

    mars4bb Private E-2

    homework

    How do we declare the variables strConnect, asOpenStatic, adLockReadOnly, adCmdTable ?
    I believe I've gotten how to do the MoveNext part but I keep getting errors from the browsers telling me that these are undefined variables....

    Our prof. had a different example and he declared those variables like this but I'm not sure why...I know that what we are using on this problem differs but could we do it in this same way?
    adOpenForwardOnly = 0
    adLockReadOnly = 1
    adCmdTable = 2

    this is what I have so far including the MoveNext can you take a look and see if it is basically right and give me an idea...well any idea...if I'm moving in the right direction...
    <%
    Option Explicit
    Dim strConnect
    %>


    <!-- #include file="12a-datastore.asp" -->
    <HTML>
    <HEAD>
    <TITLE>Using an SSI to hold the Connection String</TITLE>
    </HEAD>
    <BODY>
    <%
    Dim objRS
    Set objRS = Server.CreateObject("ADODB.Recordset")
    objRS.Open "politics", strConnect, asOpenStatic, adLockReadOnly, adCmdTable


    if objRS.eof then
    response.write ("no records")
    else
    while not objRS.eof
    strOutputString = strOutputString & "<TR>" & _
    "<TD>&nbsp;" & objRS("Name") & ": " & objRS("Office") & "</TD>" & _
    "<TD>" & objRS("Party") & "</TD>" & _
    "</TR>"
    objRS.MoveNext

    wend

    end if
    objRS.Close
    objConn.Close
    Set objRS = Nothing
    Set objConn = Nothing
    %>
    </BODY>
    </HTML>

    We are trying to do this for the first time and it's an online course so we rarely have a prof to ask questions of so please bare with us for not knowing exactly what to ask you...it is becoming clearer. Thanks. Sarah
     
  9. Kodo

    Kodo SNATCHSQUATCH

    the old addage, "There's more than one way to skin a cat" applies to programming ASP. There is more than one way to accomplish what you're doing. Your prof is being true to the book, in the actual world you'd skip that and put it into your query in one shot. 10 bucks said he forgot to tell you about including the adovbs.. you need to have a reference to that so you can use the ADO cursor and LOCK types.

    When I program a site, I ALWAYS put this in my global.asa file right at the top. You can also put this at the top any individual page. For this single page, placing it at the top should do just fine. Just note that for sites and apps, you want one place to put it and that is in the global.asa. The global.asa file loads every time the site is started (not loaded by the client).

    put this at the top
    <!--METADATA TYPE="typelib"
    FILE="c:\program files\common files\system\ado\msado15.dll" -->

    Let's pick this apart.

    PHP:
    Option Explicit
    Dim strConnect
    adOpenForwardOnly 
    0
    adLockReadOnly 
    1
    adCmdTable 
    2
    Option explicit basically means that every variable must be declared. With ASP, every variable is of type variant so the ASP environment doesn't really care that a variable has been declared or not but it helps speed things up if you declare them before hand. If not, then it auto dims variables for you. Option Explicit will also let you know when a variable has already been declared. Good practice to use this so continue to do so.

    Now, for the ado lock, cursor and command type all your prof is doing is basically assigning them a number value so you you can use 0,1,2 in your query instead of typing out adLockReadOnly etc etc. Do do this properly you would make them constants.

    PHP:
    CONST adOpenForwardOnly=0
    howerver, with the inclusion of the msado15.dll that I am having you put at the top of your page, you don't need to do this. You can use the text or the number value and it will work fine. For beginners, I recommend typing it out so you get used to it. Heck, I still type them out.


    PHP:
    Dim objRS
    Set objRS 
    Server.CreateObject("ADODB.Recordset")
    objRS.Open "politics"strConnectasOpenStaticadLockReadOnlyadCmdTable
    So, I see in your code that you're using strictly the ADO method. Nothing wrong with that at all. You've dim'd your RS and set it to open a table in your command.. Lets move on.

    PHP:
    if objRS.eof then
    response
    .write ("no records")
    else
    while 
    not objRS.eof
    strOutputString 
    strOutputString "<TR>" _
    "<TD>&nbsp;" objRS("Name") & ": " objRS("Office") & "</TD>" _
    "<TD>" objRS("Party") & "</TD>" _
    "</TR>"
    objRS.MoveNext

    wend

    end 
    if 
    This section here has me raise a question for you. You set your entire string to a variable (strOutputString). In looking at that, I don't see your <table></table> tags.. you should fix that. You've also added strOutputString to itself by doing
    PHP:
    strOutputString=strOutputString &"..."
    This is totally un-necessary. You're just doubling up your string when you do that. You only need to do that when you want to concatenate variable values together into one string.
    In this case we are not doing that since as we loop it will print one record at a time. Do you follow?

    Additionaly, you forgot to response.write out your string, so when you go to view your records you won't see anything but a pristine white page ;).

    To go one step further, let me introduce to you a technique called interleaving. Now you don't have to do this now, but you should know that it exists.

    Instead of putting your whole content into a variable, you can do something like this.

    PHP:
    if objRS.eof then
    response
    .write ("no records")
    else
    %>
    <
    table border="1">
    <%while 
    not objRS.eof%>
     <
    TR>
      <
    TD>&nbsp;<%=objRS("ID")%>: <%=objRS("FirstName")%></TD>
      <
    TD><%=objRS("LastName")%> </TD>
     </
    TR>
    <%
     
    objRS.MoveNext
    wend
    %>
    </
    table>
    <%
    end if 
    This saves you the hassle of having to watch ALL your syntax with the quotes and the &'s etc.. basically you close out the ASP tag and start your HTML and then open and close your <%%> as needed to process ASP code. Take note of it. You WILL end up using it eventually.

    Ok, so fix those things that I mentioned and post back. You're on the right track and you'll soon realize that it's much easier than it's made out to be ;)
     
  10. mars4bb

    mars4bb Private E-2

    Next Record....

    Well it's looking better. I'm now getting something printing onto the screen without errors. I really liked the interleaving you showed me. So I stuck with that. Now from my understanding the problem is wanting us to be able to flip thru the database and view one record at a time. Forward, Backwards, Beginning, End.
    The whole database is printing to the screen though. This is my code:

    <%
    Option Explicit
    Dim strConnect
    %>
    <!--METADATA TYPE="typelib"
    FILE="c:\program files\common files\system\ado\msado15.dll" -->
    <!-- #include file="12a-datastore.asp" -->
    <HTML>
    <HEAD>
    <TITLE>13a-move</TITLE>
    </HEAD>
    <BODY>
    <%
    Dim objConn, objRS

    Set objConn = Server.CreateObject("ADODB.Connection")
    Set objRS = Server.CreateObject("ADODB.Recordset")
    objConn.Open strConnect
    objRS.Open "politicians", objConn, adOpenStatic, adLockReadOnly, adCmdTable

    if objRS.eof then
    response.write ("no records")
    else
    %>
    <table border="1">
    <%while not objRS.eof%>
    <TR>
    <TD>Name:&nbsp;<%=objRS("Name")%> <TD>
    Office:&nbsp;<%=objRS("Office")%></TD>
    <TD> Party:&nbsp; <%=objRS("Party")%> </TD>
    </TR>
    <%
    objRS.MoveNext
    wend%>
    </table>
    <%
    end if
    objRS.Close
    objConn.Close
    Set objRS = Nothing
    Set objConn = Nothing
    %>
    <form action="13a-moving.asp" method="post">
    <input type=button value=|< name=First>&nbsp;&nbsp;

    <input type=button value= < name=Previous>&nbsp;&nbsp;

    <input type=button value= ">" name=Next>&nbsp;&nbsp;

    <input type=button value= ">|" name=Last>&nbsp;&nbsp;

    </form>
    </BODY>
    </HTML>

    I took out the
    "adStaticOpen = 0
    adLockReadOnly = 1
    adCmdTable = 2"
    code since it wasn't necessary right now...Like you said being new to this typing repetition will be best.

    Allright so now my next step will be to get one record on the screen at a time right? That means I'll have to set some limitation to the loop?

    Now I went ahead and added the buttons to the bottom. I know they don't function but still they are there for now. How am I going to connect those so when I hit any of the buttons such as the forward button it moves to the next record?

    When I started all of this was about as clear as mud, but it's better now. I understand how to connect and what you have to define first so that's something. It'll click eventually...
     
  11. Kodo

    Kodo SNATCHSQUATCH

    ok, I'm a little confused as to what your professor wants now. From reading the assignment it appeared that he wanted the page to display all the records using the movenext method and then moveprevious to show the previous record all in one shot. Then to also give the ability to move through the records one at a time but still show the other list? is this correct or does he just want you to move through the records only and not show the full list of records?

    Answer this so I can clarify our next move.
     
  12. mars4bb

    mars4bb Private E-2

    I believe he wants us to be able to move thru the records one at a time. The original problem that we posted was all that he gave us, and I won't get to see him until tomorrow which is when this problem is due...tomorrow night by 12:00. I also have a second part that is due. I'll post it so you can read it:

    [font=Verdana, Arial, Helvetica, sans-serif]Create one ASP page that will use the Fields Collection to display all of the data in the politics.mdb, politicians table. Ensure that the field names are displayed. [/font]

    Now this assignment seems to be what we do have so far. All of the information is displayed and I have field names on there.

    I think we need to try to be moving from one record to another in that first assignment, and I'm guessing that means not to have everything displayed just one record at a time. So I guess that's how we need to proceed....Thanks!
     
  13. Kodo

    Kodo SNATCHSQUATCH

    ok, well.. I won't be around much tomorrow so I don't want to leave you hanging but I don't want to flat out give you the answer either. So I'm caught in a moral dilemma.

    I have the sample code below which will do what you want, but I didn't comment anything and it's using my own database. So you're going to have to go through and make sure objConn is in there and put your provider in there etc..

    I didn't comment anything because I want you to figure out what I did and why and put the comments in for your professor. I'll peek in if I have the time to answer questions about what I did and why but the code is complete.

    You showed interest in trying and that is the only reason why I am giving you the whole thing. If it were someone just begging for the code then I wouldn't have done squat. Aside from that, you got some experience in looping so you learned something anyway.

    here you go:
    PHP:
    <!--METADATA TYPE="typelib"
    FILE="c:\program files\common files\system\ado\msado15.dll" -->
    <!-- 
    #include virtual="dconn.asp" --> 
    <HTML>
    <
    HEAD>
    <
    TITLE>13a-move</TITLE>
    </
    HEAD>
    <
    BODY>
    <%
     
    Set objRS Server.CreateObject("ADODB.Recordset")
    objRS.Open "users"ConnadOpenStaticadLockReadOnlyadCmdTable
    if objRS.eof then
    response
    .write ("no records")
    else
    Dim CurRec,RecCount,RecIndex
    RecCount
    =objRS.RecordCount
    RecIndex
    =request.querystring("rec")
     
    if 
    Cint(RecIndex)<0 then 
            recindex
    =0
    end 
    if
     
    if 
    Cint(RecIndex) > Cint(RecCount)-1 then
            recindex
    =RecCount-1
    end 
    if
     
     
    objRS.move(RecIndex)
    CurRec=objRS.bookmark
     
    %>
    <
    table border="1">
    <
    TR>
             <
    TD>Name:&nbsp;<%=objRS("ID")%> <TD
             <
    TDOffice:&nbsp;<%=objRS("FirstName")%></TD
             <
    TDParty:&nbsp; <%=objRS("LastName")%> </TD>
    </
    TR>
    </
    table>
    <%
     
     
    objRS.Close
    Conn
    .Close
     
    Set objRS 
    Nothing
    Set Conn 
    Nothing 
    %>
    <
    form method="post" action="" >
    <
    input type="submit" value="|<" name="First" onclick="this.form.action='<%=script_name%>?rec=0'";>&nbsp;&nbsp
    <
    input type="submit" value"<" name="Previous" onclick="this.form.action='<%=script_name%>?rec=<%=Cint(CurRec)-2%>'";>&nbsp;&nbsp
    <
    input type="submit" value">" name="Next" onclick="this.form.action='<%=script_name%>?rec=<%=Cint(CurRec)%>'"; >&nbsp;&nbsp
    <
    input type="submit" value">|" name="Last" onclick="this.form.action='<%=script_name%>?rec=<%=Cint(RecCount)-1%>'";>&nbsp;&nbsp
     
     
    <%
    end if %>
    </
    form>
    </
    BODY>
    </
    HTML>

    FYI: your buttons for forms should not be "button" type but "submit" type, otherwise nothing is going to happen.

    OnClick is a javascript action for the specific button clicked.

    <%=Script_Name%> just takes the name of the current page that you are on.
    Good luck

    p.s. here's some nice references for you

    http://www.inthehand.com/documentation/adoce/InTheHand.AdoceNet.RecordsetProperties.html

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vbscripttoc.asp
     
  14. mars4bb

    mars4bb Private E-2

    Thanks...

    hey there...I wanted to thank you for all the help you gave us...we finally got the problem working. Your code of course helped us in that area. I wanna be able to learn this stuff and not just get a grade in the class....after all if you don't learn what you need to know...getting a job doesn't necessarily mean you'll keep the job...but all your help was definately appreciated. I still feel like I learned more than I knew when I started so that's a good thing. The referrences you gave, I'm sure will came in handy when doing the major project at the end of the semester...Take Care, Sarah
     
  15. Kodo

    Kodo SNATCHSQUATCH

    I'm glad to have helped. Making water from mud can be difficult at times, but trust me, it will click one day and you'll see just how much MORE there is to learn ;).

    You should pick up BEGINNING ASP by WROX PRESS : ISBN 1-861003-38-2

    It's a good book to get you off the ground quickly. Though some of their technique is strictly by the "book" (pardon the pun), it's a great overall reference as well.

    Good Luck. If you have any more questions, let me know.
     
  16. mars4bb

    mars4bb Private E-2

    Book...

    I went to go get that book on half.com and that's actually already the book we are using for class. Just didn't recognize the name of the book without seeing the dude's faces on the cover...So maybe we have a few things going for us in class after all. :) If you happen to think of anymore suggestions...we're game...
     
  17. Kodo

    Kodo SNATCHSQUATCH

    1 person likes this.
  18. Kodo

    Kodo SNATCHSQUATCH

    so, how'd did it go. Did you pass?
     
  19. goldfish

    goldfish Lt. Sushi.DC

    I agree Wrox books are the best :D

    I wanna know how you did too, ive just read through this thread and im interested!
     
  20. mars4bb

    mars4bb Private E-2

    hey there....yes I did pass...actually made a 10 out of 10 on both assignments...Thanks to you....now we are starting to work on the big main project. I did get in that Professional ASP book you suggested so hopefully it will help out some, and I hope you don't mind cause I'm sure I'll have more questions...just maybe not quiet so many this time... :)
     
  21. Kodo

    Kodo SNATCHSQUATCH

    Happy to hear that you got a 100%.!!
    I'm sure you'll find the professional book a little more paced than the beginners book but don't worry about the later chapters until you get into ASP more.

    Post a new thread with any new questions and we'll work them out :)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds