Murray Brown
asked on
ASP.net print results of database SQL statement
Hi
I am using the code shown in the code section below to get all the records
from a table in a database held online. How do I print my results on the webpage?
Do I use "Response"
I am using the code shown in the code section below to get all the records
from a table in a database held online. How do I print my results on the webpage?
Do I use "Response"
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'// define a connection to the database
Dim cn As New OleDbConnection(ConfigurationManager.ConnectionStrings("WhatEverNameYouWant").ConnectionString)
'// define the sql statement to execute
Dim cmd As New OleDbCommand("SELECT * FROM [Contacts]", cn)
Try
'// open the connection
cn.Open()
'// execute the sql statement
Using reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While reader.Read()
'// this loops through all of the returned records
End While
End Using
Catch ex As Exception
Response.Write(ex.Message)
Finally
If cn.State <> ConnectionState.Closed Then
cn.Close()
End If
End Try
End Sub
One way would be to issue
RESPONSE.WRITE(whatever)
inside your loop. That will give you a page full of whatever is coming out of the database. It won't be formatted in any particular way, but you'll be able to read it.
Just for testing, you might try
RESPONSE.WRITE(whatever & "<br />")
so each item out of the database is on its own line in the page.
RESPONSE.WRITE(whatever)
inside your loop. That will give you a page full of whatever is coming out of the database. It won't be formatted in any particular way, but you'll be able to read it.
Just for testing, you might try
RESPONSE.WRITE(whatever & "<br />")
so each item out of the database is on its own line in the page.
ASKER
Thanks but how do I write each record. What do I put where you have put "whatever"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks but how do I write each record in each response?
...
While reader.Read()
Response.Write(reader("column_name") & "<br />")
End While
...
As both [carl_tawn] and I note, this is a quick and dirty method. It will give you what you asked for, but it won't be formatted (look nice).
While reader.Read()
Response.Write(reader("column_name") & "<br />")
End While
...
As both [carl_tawn] and I note, this is a quick and dirty method. It will give you what you asked for, but it won't be formatted (look nice).
If you are just starting out with ASP.Net then it will really be worth your while going through the samples here:
http://www.asp.net
It will teach you the basics of working with data, binding to controls and the types of data presentation controls available.
http://www.asp.net
It will teach you the basics of working with data, binding to controls and the types of data presentation controls available.
ASKER
Thanks very much. Sorry for late reply
ASKER
Thanks carl_tawn. I just hosted my first online Access darabase> Really appreciate all your help and patience. Murray
No problem. Glad you got there in the end :)
Thanks