Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple example of showing query results on a web page

Status
Not open for further replies.

mauu

MIS
May 23, 2007
13
0
0
US
I've a ton of experience with sql server, but next to nothing with creating web pages and I want to learn. For me I've always learned easily from examples so if anyone can give me a super basic code example from start (assume i have the stored proc already) to finish (able to see the results on the page) how to implement this I would be a very happy man. :D

Does all the code go right in the source of the page? or do i need to somehow call some script that gathers the info?

Once I have the info, how do i get it to show all returned records?

Hope I'm not asking too much, but I've been scouring the internet and putsing on my own for quite a while now and keep going in circles.
 
The answer to your question depends on the technology you plan on using for your web page. You could use ASP, ASP.Net, Cold Fusion, PHP, etc... The method for displaying the output of a stored procedure will be different for each.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
haha, monksnake you're the man! :p

so, since the example are in ?asp? looks like I'll be using asp. :p

So, with the example below, how do I get it to work with my data..ie can i just through it in notpad save as test.html then open it with fire fox to see it? ( I tried this and it didnt' work but the server i'm querying is not local so I used
Code:
conn.Open(Server.Mappath("/server.domain.com/db/northwind.mdb"))

Didn't work though....

Code:
<html>
<body>

<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/server.domain.com/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM Customers"
rs.Open sql, conn
%>

<table border="1" width="100%" bgcolor="#fff5ee">
<tr>
<%for each x in rs.Fields
    response.write("<th align='left' bgcolor='#b0c4de'>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
    <tr>
    <%for each x in rs.Fields%>
       <td><%Response.Write(x.value)%></td>
    <%next
    rs.MoveNext%>
    </tr>
<%loop
rs.close
conn.close
%>
</table>

</body>
</html>

 
Your connection string is not quite right. Visit this page for a list of connection strings for different DB programs.


Here's an example of what we do at work for SQL Server
Code:
var strConn = "Provider=SQLOLEDB;Data Source=SERVERNAME;Initial Catalog=DATABASENAME;User Id=YOURUSERTHATHASPERMISSIONTOTHISDATABASE;Password=XXXXXXX";
var oConn = Server.CreateObject("ADODB.Connection");
oConn.Open(strConn);

Provider isn't needed I'm pretty sure, and fill in the necessary data where I have capital letters above, except for in Provider, leave that the same.


[monkey][snake] <.
 
Oh one more thing, you are using ASP with VBScript, so you use Dim and Set instead of var.

[monkey][snake] <.
 
hmmm, not quite working yet, but I'll keep trying. Mean time I obviously can't put usernames and passwords in the source of my page, so how do I code my page to grab the stuff from the asp file?
 
I'm still doing something wrong and I can't figure out what. when I open the .asp file using fire fox it renders a table with no data and shows the code that's supposed to be connecting to the database as text on the page. kinda like it dosn't realize its supposed to be code but rather thinks its just a label. Do I have to preface the code with <%asp or something?

Code:
<html>
<body>

<%
set strConn = "Provider=SQLOLEDB;Data Source=srvr1.strtc.com;Initial Catalog=oen;Trusted_Connection=yes";

set oConn = Server.CreateObject("ADODB.Connection");

oConn.Open(strConn)

set sql="SELECT * FROM cust"

rs.Open sql, strConn 
%>

<table border="1" width="100%" bgcolor="#fff5ee">
<tr>
<%for each x in rs.Fields
    response.write("<th align='left' bgcolor='#b0c4de'>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
    <tr>
    <%for each x in rs.Fields%>
       <td><%Response.Write(x.value)%></td>
    <%next
    rs.MoveNext%>
    </tr>
<%loop
rs.close
conn.close
%>
</table>
 
hmmm...was just thinking...do i need to install some sort of asp executing program on the server for it to work?
 
Typically you will have to install IIS on your machine. IIS contains ASP support.

You can go to the control panel -> add/remove programs -> Add/remove Windows Components.

You will be able to click on Internet Information Services (IIS).

After you install that, you should be able to use ASP and .NET stuff if you'd like.

[monkey][snake] <.
 
iis is now installed and opening the asp file still just gives me text. Do I have to associate .asp files with some program?
 
ah, figured out how to get asp files to work ....now I'm getting errors...Can you see where?

Code:
<html>
<body>

<%
set strConn = "Provider=SQLOLEDB;Data Source=srvr1.strtc.com;Initial Catalog=oen;Trusted_Connection=yes";

set oConn = Server.CreateObject("ADODB.Connection");

oConn.Open(strConn)

set sql="SELECT * FROM cust"

rs.Open sql, strConn
%>

<table border="1" width="100%" bgcolor="#fff5ee">
<tr>
<%for each x in rs.Fields
    response.write("<th align='left' bgcolor='#b0c4de'>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
    <tr>
    <%for each x in rs.Fields%>
       <td><%Response.Write(x.value)%></td>
    <%next
    rs.MoveNext%>
    </tr>
<%loop
rs.close
conn.close
%>
</table>
 
You may want to ask this in the ASP forum.

Click here: forum333

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top