Library tutorials & articles
Beginning Active Server Pages
Using Databases 2
Returning to our earlier example, we've already collected the username and email of the person who wants to register using a form. Now we need to build an SQL Insert string to insert this information into a table. However, before that, we need to quickly go into our database, and create a new table. Use the following information:
| Name | Data Type | |
| Table Name | MyUsers | |
| Fields | ID (AutoNumber/Identity) | AutoNumber (Access) or Identity (MS SQL). Primary Key |
| Name (String/VarChar) | Text (Access) or VarChar (MS SQL). Max Length: 20 | |
| Email (String/VarChar) | Text (Access) or VarChar (MS SQL). Max Length: 100 |
We already know that the information we want from the form is in the Request.Form variable, so lets have a go at creating the SQL string.
First a quick reminder of the SQL Insert syntax:
INSERT INTO TableName (Field1, Field2, ...) VALUES
(Field1Value, Field2Value, ...)
So, we can start with the following code:
sSQL = "INSERT INTO MyUsers (Name, Email)"
Next, we need to provide the information of the new user:
sSQL = sSQL & " VALUES ('" & Request.Form("Name")
& "','" & Request.Form("Email") & "')"
For anyone not used to using SQL in an ASP page that might seem a bit confusing, so lets take a look at what the final value of sSQL will be if the Name was "James" and the email was "james@vbweb.co.uk":
INSERT INTO MyUsers (Name, Email) VALUES ('James','james@vbweb.co.uk')
|
and using sName when building up the Insert string, instead of Request.Form("Name"). |
Finally, you will need to execute this SQL statement:
cConn.Execute sSQL
... and that's almost it! All we need to do is put it all together. Below is the completed source code of inputform.asp:
<%
'Open the db
connection Set cConn = Server.CreateObject ("ADODB.Connection")
cConn.Open "test_db","",""
'Build the SQL string
sSQL = "INSERT INTO MyUsers (Name, Email)"
sSQL = sSQL & " VALUES ('" & Request.Form("Name")
& "','" & Request.Form("Email") & "')"
'Execute the SQL
cConn.Execute sSQL
'Output message
Response.Write "<p>Thankyou for signing up.
Your information has been added to our database!</p>"
<form action="forminput.asp" method="POST">
Your Name <input type="text" name="Name"><br>
Your Email <input type="text" name="Email"><br>
<input type="hidden" name="Posted" value="1">
<input type="submit" value="Sign Up">
</form>
<% End If %>
Related articles
Related discussion
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
Help to Call ASP function from onclick event in HTML to pass an array
by vka (0 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
When I say I'm an ASP beginner, I mean it! I've only written one trivial bit of code, and got a blank page. I see now that's described in the article snippet below. I did indeed go to the saved .asp page on my hard drive!
So my questions:
I have XP, so IIS is presumably installed. In the snippet below, are 'localhost' and c:\inetpub\wwwroot the actual strings to type in? For example, I currently don't have a folder called "inetpub". Do I have to create one? And where on my actual server (starman.co.uk) should I put any ASP pages?
Alternatively, is there a good introductory book you can recommend?
http://localhost/orhttp://PCName/. The physical location of your web site is by defaultC:\inetpub\wwwroot. Save your ASP pages there, and view them in a browser by visiting the URLs above.hi,
its first i started ASP.I want to know how to save the files.whats the extensions...Where can i get the sample programs
Are you running IIS ? And are you viewing it in your browser via the correct URL? (ie something starting with http:// rather than file:// ) ?
It does - you just can't see it
We've got an ISAPI filter that rewritse /show/1010/ to something like /show.aspx?id=1010
Let's see if ASP works!<br>
<%
Response.Write "Yep!"
%>
</html>
"The VB code can act on information passed to the page, such as from an internet form or a querystring (this is data passed in the URL after the ? ... take a look at this pages URL!)"
http://www.developerfusion.com/show/1010 doesn't have a question mark in it...
you can not include a file from a remote storage site other than the one your website is being hosted on , like writing <!--#include file = " www.someplace.com/somefile.inc " --> but by using asp tear codes you can if you have any support this features.
<!--#include file = " www.someplace.com/somefile.inc " -->
or even less the " virtual " call which starts searching the virtual directory the website is stored on.
What now?
Do I need to write a routine to access the file remotely then spilling it onto the page using response.write???
I am very new to ASP technology.
I am in a need to use DLLs (written in C#) in ASP Pages.
Could anyone suggest me how to proceed.
Regards,
MAK,
thanks, but it wasn't the sql. i did that. it's long since solved.
chk your SQL statement. Stop before execting cConn.Execute sSQL . Print sSQL and try executing the same in the backend. If it doesnt work then you need to change the SQL statement.
i solved the problem a while ago, but i forgot how.
heppens to be that i have the same prolem.
I'm beginner of ASP.I make my thesis with ASP.But I've got problem for building project in Visual InterDev.I'd already installed IIS5.It shows web server can't connect.How I do?If it's cause of IIS error,please explain me step by step.let me know solution during five days.please help me.
It doesn't work it all. I created the table using Access and set of the OBDC fine. What's wrong?
I get this error when I submit forminput.asp:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Unknown
/forminput.asp, line 12
What's wrong? I even copied the code over exactly.
the error's with this line:
cConn.Execute sSQL
I tried this code out exactly, but I get an INSERT INTO syntax error. I have also tried using the rs.AddNew to add to my database but I get a read-only error. I have tried changing the lock type and cursor types around but I can't get anything to work.
pls see t he comment
U Have to include a file name adovbs.inc file i yr asp page. thenu can use the addnew and update methods
One thing to note, this only works on pages which have been posted to
This thread is for discussions of Beginning Active Server Pages.