Sign in to follow this  
blackbirdblackbird1

[web] ASP Question!

Recommended Posts

It's the same, you'll be calling a variable. I'm also PHP/MySQL oriented. I've been fighting the transition to ASP but I'm finding it almost impossible to apply for other web development related positions at other companies due to my lack of knowledge and experience with the language.

Share this post


Link to post
Share on other sites
Well, perhaps you should pay better attention to the ads and have a look out for the words "PHP developer"? That's what my new job's advert said. Also, you could call up the compnay that placed the ad and simply ask "Is this about a PHP or ASP position" and if they say ASP, just tell them you'll be sending in your resume and motivation promptly.

I don't know how it is in the states but there are plenty of PHP positions on this side of the pond.

Share this post


Link to post
Share on other sites
Quote:
Original post by blackbirdblackbird1
I need someone to write out a script of how you would do something like this:

http://url.com/default.asp?id=12341324

I'd love to give an answer, if only I understood the question. :/
What exactly is it that you want to do?

Share this post


Link to post
Share on other sites
I attempt most positions, but the two areas I have intrest in (winchester, va that I live in and seattle, wa which would be a nice change of scenary) both tend to use microsoft technologies.

I belive in my current work place I'll stress the importance of diversifying our abilities so I can add the latter to my resume. That will probably hlep to search for php instead of web development, it may save some time.

Share this post


Link to post
Share on other sites
I think it's best to find yourself some ASP tutorials. It's a fairly 'old' scripting language, so there should be tons of learning material online.

Besides if you already familiar with PHP scripting, learning some basic ASP shouldn't be a problem. The syntax (VB) is different, and you have actually less functions to work with, but the idea is the same.

Anyway, to get you started:
<%
Option Explicit ' not necessary, but good practice

Dim id

' get id parameter, in this case we're checking if it's a number
If IsNumeric(Request("id")) Then
id = CLng(Request("id"))
Else
id = 0
End If
%>
<html>
<body>
<%

Const USE_DATABASE = False

If USE_DATABASE Then

' using a database
Dim conn
Dim rs

' initialize connection and recordset
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")

conn.Open("your connection string here")

rs.Open "SELECT TOP 1 title FROM Table WHERE id = " & id, conn

' if result set contains a record, print it
If Not rs.EOF Then
Response.Write("<h1>" & rs("title") & "</h1>")
End If

rs.Close()

Else

' show content based on id (hard-coded)
Select Case id

Case 1:
Response.Write("<h1>This is page one</h1>")
Case 2:
Response.Write("<h1>This is page two</h1>")
Case 3:
Response.Write("<h1>This is page three</h1>")
Case Else
Response.Write("<h1>This is the default page</h1>")

End Select
End If
%>

<a href="default.asp">default page</a> | <a href="default.asp?id=1">page one</a> | <a href="default.asp?id=2">page two</a> | <a href="default.asp?id=3">page three</a>

</body>
</html>

Note that I haven't tested it, and that it's an extremely simple example.
The code above will parse the id parameter from the querystring and store it in a variable id. How to then use this id is up to you. In my sample I have included two options: one is using a database to select the content to be displayed, the second is using a switch statement and some hard-coded text (notice the constant to switch between these two).

Share this post


Link to post
Share on other sites
YAY!! So far I have gotten it to work!

I was wondering how would you call a variable that has already been extracted from a database like in PHP: <? $variable ?> So that you can call it several times outside the php. And how would I change the page title according to the teachers name.

Share this post


Link to post
Share on other sites
No offense, but I am not going to write all your code. I gave you an example to get you going, but it still requires some effort from you. Like I and others already pointed out, find a book or go through some online tutorials to get a better understanding of ASP.

I'll be a nice guy and answer your last two post: [smile]

Quote:
Original post by blackbirdblackbird1
I was wondering how would you call a variable that has already been extracted from a database like in PHP: <? $variable ?> So that you can call it several times outside the php. And how would I change the page title according to the teachers name.

You mean who to write to the reponse stream?

<%= myVariable %>



Quote:
Original post by blackbirdblackbird1
How would you have it display a message if the ID is not found in the database?


Take a look at this section of the code. It checks if a record has been found. This is where you can handle your not-found case.


' if result set contains a record, print it
If Not rs.EOF Then
Response.Write("<h1>" & rs("title") & "</h1>")
End If

' if result set contains a record, print it (NEW VERSION)
If Not rs.EOF Then
Response.Write("<h1>" & rs("title") & "</h1>")
Else
Response.Write("<h1>Entry not found</h1>")
End If



Share this post


Link to post
Share on other sites
When I add the "Entry not Found" message I get this:


Error Type:
ADODB.Field (0x80020009)
Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.
/faculty/history.asp


I only get this error when there is no entry with that ID.


Dim conn
Dim rs

' initialize connection and recordset
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
conn.Provider="Microsoft.Jet.OLEDB.4.0"

conn.Open("c:/inetpub/ahsftp/AHS/design/faculty/teacher.mdb")

rs.Open "SELECT TOP 1 name, photo, quote, p_1, p_2, p_3, p_4, p_5, p_6, p_7, subject, room, office_hours, voice_mail, email, fav_sports, fav_subj, fav_food, fav_movie, fav_music FROM history WHERE id = " & id, conn

' if result set contains a record, print it (NEW VERSION)
If Not rs.EOF Then
Response.Write("<h1>" & rs("name") & "</h1>")
Else
Response.Write("<h1>Either the Teachers info has not been added yet or you have been given an invalid teacher ID.</h1>")
End If
%>






Thanx so much for all your help!

Share this post


Link to post
Share on other sites
I, like most others here, don't know crap about ASP, but aren't you able to 'select' your programming language, something like: "<script language='x' runat='server'>"? If you like your C-style syntax, you can try using C#, but that doesn't have anything much to do with the question at hand (though, from the ASP code I've seen so far, the methods you have to take to get to that information looks like crap to me).
Or is that ASP.NET-specific?

Share this post


Link to post
Share on other sites
Quote:
Original post by deadimp
I, like most others here, don't know crap about ASP, but aren't you able to 'select' your programming language, something like: "<script language='x' runat='server'>"? If you like your C-style syntax, you can try using C#, but that doesn't have anything much to do with the question at hand (though, from the ASP code I've seen so far, the methods you have to take to get to that information looks like crap to me).
Or is that ASP.NET-specific?

ASP != ASP.NET, ASP is VBScript only.

Share this post


Link to post
Share on other sites
Quote:
Original post by WanMaster
Quote:
Original post by deadimp
I, like most others here, don't know crap about ASP, but aren't you able to 'select' your programming language, something like: "<script language='x' runat='server'>"? If you like your C-style syntax, you can try using C#, but that doesn't have anything much to do with the question at hand (though, from the ASP code I've seen so far, the methods you have to take to get to that information looks like crap to me).
Or is that ASP.NET-specific?

ASP != ASP.NET, ASP is VBScript only.


well, JScript can be used, but it is so poorly documented and supported that it might as well not be available.

Share this post


Link to post
Share on other sites
I think you would just need to diminuate (don't know why they chose that word) and define the variable:
<%
Dim x
x=Date() 'That, or Set x=New Date(), or however VB works with that
%>


This is a VBScript-specific question

Share this post


Link to post
Share on other sites
Function GuaranteeCLng(sNum, iDefault)
If IsNumeric(sNum) Then
GuaranteeCLng = CLng(sNum)
Else
GuaranteeCLng = iDefault
End If
End Function

'This is a great function for seeing whats going on
Function DisplayRequest()
Dim i
Dim iMax

iMax = Request.ServerVariables.Count
For i = 1 To iMax
Response.Write("")& vbNewLine
Next
iMax = Request.Form.Count
For i = 1 To iMax
Response.Write("")& vbNewLine
Next
iMax = Request.QueryString.Count
For i = 1 To iMax
Response.Write("")& vbNewLine
Next
End Function

'This function shows a date in DD/MM/YYYY format
Function DisplayDate(d)
Dim s

If IsDate(d) Then
If Day(d)<=9 Then
s=s & "0"
End If
s=s & Day(d) & "/"
If Month(d)<=9 Then
s=s & "0"
End If
s=s & Month(d) & "/" & Year(d)
End If
DisplayDate=s
End Function


Dim sID
Dim iID
Dim oConn
Dim oRs
Dim dDOB

sID = Trim(Request.QueryString.Item("ID"))
iID = GuaranteeCLng(sID, 0)

If iID = 0 Then
'do nothing
Else
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.ConnectionString = "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=ASPTest;User ID=ASPTester;Password=PASSWORD;"
Set oRs = oConn.Execute("SELECT history.* FROM history WHERE history.ID = " & iID
If oRs.EOF Then
Else
...
dDOB = CDate(oRs("DOB").Value)
...
Response.Write("Date of Birth: " & DisplayDate(dDOB))
End If
oRs.Close
Set oRs = Nothing
oConn.Close
Set oConn = Nothing
End If
Call DisplayRequest()

Share this post


Link to post
Share on other sites
Quote:
Original post by ROBERTREAD1
Function GuaranteeCLng(sNum, iDefault)
If IsNumeric(sNum) Then
GuaranteeCLng = CLng(sNum)
Else
GuaranteeCLng = iDefault
End If
End Function

'This is a great function for seeing whats going on
Function DisplayRequest()
Dim i
Dim iMax

iMax = Request.ServerVariables.Count
For i = 1 To iMax
Response.Write("")& vbNewLine
Next
iMax = Request.Form.Count
For i = 1 To iMax
Response.Write("")& vbNewLine
Next
iMax = Request.QueryString.Count
For i = 1 To iMax
Response.Write("")& vbNewLine
Next
End Function

'This function shows a date in DD/MM/YYYY format
Function DisplayDate(d)
Dim s

If IsDate(d) Then
If Day(d)<=9 Then
s=s & "0"
End If
s=s & Day(d) & "/"
If Month(d)<=9 Then
s=s & "0"
End If
s=s & Month(d) & "/" & Year(d)
End If
DisplayDate=s
End Function


Dim sID
Dim iID
Dim oConn
Dim oRs
Dim dDOB

sID = Trim(Request.QueryString.Item("ID"))
iID = GuaranteeCLng(sID, 0)

If iID = 0 Then
'do nothing
Else
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.ConnectionString = "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=ASPTest;User ID=ASPTester;Password=PASSWORD;"
Set oRs = oConn.Execute("SELECT history.* FROM history WHERE history.ID = " & iID
If oRs.EOF Then
Else
...
dDOB = CDate(oRs("DOB").Value)
...
Response.Write("Date of Birth: " & DisplayDate(dDOB))
End If
oRs.Close
Set oRs = Nothing
oConn.Close
Set oConn = Nothing
End If
Call DisplayRequest()

Glad I learned php instead.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this