Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 1 of 1
  1. #1
    Senior Member Kings's Avatar
    Join Date
    Feb 2002
    Posts
    129
    Member #
    19
    Introduction
    I already showed you how to get data from a database using ASP, now I'm going to show you how to do it using ASP.NET
    In this tutorial I'm going to use a Access Database.

    First make a connection
    Like I said in my other tutorial, we first need to make a connection to our database.

    Code:
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>
    <script language="VB" runat="server">
    sub Page_Load(sender as Object, e as EventArgs)
     Dim connString as String
    connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
                       "C:\Inetpub\wwwroot\yourdb.mdb;"
      
    Dim objConnection as OleDbConnection
    objConnection = New OleDbConnection(connString)
    objConnection.Open()   'open the connection
        
    'rest of the code
    We have opened our connection, and we can now use the connection. We included System.Data.OleDb so we can use OleDbConnection.

    Selecting Data

    We want to select data now, from our table. We're going to use the following to do that.

    Code:
    'Specify our Query
    Dim strQuery as String = "SELECT Row1, Row2 FROM Table1"
       
    'Create the Command object
    Dim objCommand as OleDbCommand
    objCommand = New OleDbCommand(strQuery, objConnection)
    Now we have opened a connection to the database, and selected our data, the only thing left to do is display it.

    Setting Datareader

    Displaying database data through ASP.NET is different from ASP. In ASP you would loop until objRS.EOF was True. Something like this;

    Code:
    While NOT objRS.EOF
    'do code
    objRS.MoveNext
    Loop
    In ASP.NET it's done differently. It uses a datareader to read all the data, and then display it in a datagrid, or loop through the datareader.

    Code:
    ' Set an OleDbDataReader to the results
    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
    Now our Datareader has our data, and we're ready to display it.

    Display Data

    Now we're going to display the data, using a datagrid.

    Code:
    'Put the data in our datagrid
    dgDB.DataSource = objDataReader
    dgDB.DataBind()    
        
    'Close the datareader/db connection
    objDataReader.Close()
    
    'Close our sub
      end sub
    </script> 
    
    <asp:DataGrid id="dgDB" runat="server" />
    That was it! We have displayed out data in a datagrid.
    But what if you want to check the data, and make your own table?

    Looping Through The Datareader

    You can also loop through the datareader, and check values, or make your own table.

    Code:
    While objDataReader.Read()
    'your code
    ' e.g = objDataReader("Row1")
    End While
    Complete Code

    Code:
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>
    <script language="VB" runat="server">
    sub Page_Load(sender as Object, e as EventArgs)
    Dim connString as String
    connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
                       "C:\Inetpub\wwwroot\yourdb.mdb;"
      
    Dim objConnection as OleDbConnection
    objConnection = New OleDbConnection(connString)
    objConnection.Open()   'open the connection
        
    
    'Specify our Query
    Dim strQuery as String = "SELECT Row1, Row2 FROM Table1"
       
    'Create the Command object
    Dim objCommand as OleDbCommand
    objCommand = New OleDbCommand(strQuery, objConnection)
      
    
    ' Set an OleDbDataReader to the results
    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
    
    'Put the data in our datagrid
    dgDB.DataSource = objDataReader
    dgDB.DataBind()    
        
    'Close the datareader/db connection
    objDataReader.Close()
    
    'Close our sub
      end sub
    </script> 
    
    <asp:DataGrid id="dgDB" runat="server" />
    That was it!
    K i n g s

    Several handy ASP Resources:
    4guysfromrolla | aspin | ASP101 | AspIt | learnasp

    Several handy ASP.NET resources:
    ASP.NET | .NET Experts | O'Reilly .NET | AspIt | 4guysfromrolla

    Several paid ASP/ASP.NET webhosts:
    Brinkster | Maximum ASP | Clicktech | Nodehosting | Uplinkearth | Webhost4life

  2.  


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

hot to retrieve data with oldb and show on asp

,

how to display data in aspx using loop

Click on a term to search for related topics.
All times are GMT -6. The time now is 11:14 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com