Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ASP.net print results of database SQL statement

Posted on 2010-11-22
10
388 Views
Last Modified: 2012-06-21
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"
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

Open in new window

0
Comment
Question by:murbro
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:meispisces
ID: 34190848
"Print results on webpage".You mean to print the results only or the entire webpage with results. Please explain what you wish to do so in detail..

Thanks
0
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 34190856
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.
0
 

Author Comment

by:murbro
ID: 34190926
Thanks but how do I write each record. What do I put where you have put "whatever"
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34190954
Just dumping everything using Response.Write is how you would do things in Classic ASP, we've moved on bit since then :)

The quickest way to get your data up on screen would be to bind it to a GridView. Drop a GridView onto your page from the Toolbox. Then replace these lines:

                While reader.Read()
                    '// this loops through all of the returned records
                End While

Open in new window

With:
GridView1.DataSource = reader
GridView1.DataBind()

Open in new window

0
 

Author Comment

by:murbro
ID: 34191173
Thanks but how do I write each record in each response?
0
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 34191222
...
   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).
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34191272
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.
0
 

Author Closing Comment

by:murbro
ID: 34199136
Thanks very much. Sorry for late reply
0
 

Author Comment

by:murbro
ID: 34199429
Thanks carl_tawn. I just hosted my first online Access darabase> Really appreciate all your help and patience. Murray
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34199487
No problem. Glad you got there in the end :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IIS redirect 1 77
EF5: Question about Metadata Artifact Processing 4 23
imap read mail 1 34
Footer for each row on Gridview 2 33
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question