Jump to content

Data from Access database doesn't load


denny911

Recommended Posts

I will have lots of links (in this form: http://www.mysite.info/news/details.asp?ID=xxx) on my site. When a user clicks such a link, the page details.asp loads and displays the detailed article for some news.For example:TITLE OF SOME NEWS ARTICLEThis is the content of the news article. It should be loaded from the database I created for news to be stored.Now, I have created a database called "news-database.mdb" which contains a table named "news_tbl". The table's fields are shown below:________________________________________________ID | news_title | news_content |------------------------------------------------------------------------------------101 | title 101 | content 101 |202 | title 202 | content 202 |303 | title 303 | content 303 |and so on... |------------------------------------------------------------------------------------I've tried putting this code in the "details.asp" page:<% dim ID ID = Request.QueryString ("ID") set conn = Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open(Server.Mappath("news-database.mdb"))set rs = Server.CreateObject("ADODB.recordset")dataSQL = "SELECT * FROM news_tbl WHERE ID = " & ID & ""rs.Open dataSQL, conn%>And this code where I want my data to appear:<% Response.Write(dataSQL) %>This is supposed to close the connection:<%rs.closeset rs = nothingconn.closeset conn = Nothing%>The problem is: when I click the link, let's say http://www.mysite.info/news/details.asp?ID=202, instead of data from the database's table record with the ID=202, it is displayed: SELECT * FROM news_tbl WHERE ID = 202I tried to replace this piece of code: dataSQL = "SELECT * FROM news_tbl WHERE ID = " & ID & "" with this one: dataSQL = "SELECT news_content FROM news_tbl WHERE ID = " & ID & "" but it just doesn't seem to make any difference since the result is the same: SELECT news_content FROM news_tbl WHERE ID = 202 and NOT: content 202 (as it is in the database)Can anyone tell me what should I do in order to make this work, that is, when ID in link's querystring is 202, to have displayed record from my database..I will really appreciate if someone rewrites this code to make it work..Thanks!Denis K.

Link to comment
Share on other sites

dataSQL is a variable that only contains your query string. You use it to create your recordset (that contains the data you want to display). Now if you want to display a particular field of your recordset you call it like this :

Response.Write(rs("news_content"))

Here is your code :

<% Dim ID, conn, rs, dataSQLID = Request.QueryString ("ID")Set conn = Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open(Server.Mappath("news-database.mdb"))Set rs = Server.CreateObject("ADODB.recordset")dataSQL = " SELECT news_content FROM news_tbl WHERE ID = '" & ID & "' "rs.Open dataSQL, conn'here you just use a loop that runs trought your recordset, it stops when it reach the end of the recordset (End Of File) Do While Not rs.EOFResponse.Write(rs("news_content")) rs.MoveNextLooprs.closeset rs = nothingconn.closeset conn = Nothing%>

Happy programming :)

Link to comment
Share on other sites

dataSQL is a variable that only contains your query string. You use it to create your recordset (that contains the data you want to display). Now if you want to display a particular field of your recordset you call it like this  :
Response.Write(rs("news_content"))

Here is your code :

<% Dim ID, conn, rs, dataSQLID = Request.QueryString ("ID")Set conn = Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open(Server.Mappath("news-database.mdb"))Set rs = Server.CreateObject("ADODB.recordset")dataSQL = " SELECT news_content FROM news_tbl WHERE ID = '" & ID & "' "rs.Open dataSQL, conn'here you just use a loop that runs trought your recordset, it stops when it reach the end of the recordset (End Of File) Do While Not rs.EOFResponse.Write(rs("news_content")) rs.MoveNextLooprs.closeset rs = nothingconn.closeset conn = Nothing%>

Happy programming  :)

i just wanted to thank you for your help. this code has completely done what i wanted it to do.Thanks again, Jerome!
Link to comment
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
×
×
  • Create New...