Home

  Fitness articles

  ASP.Net articles

  SEO (Search Engine Optimization) Articles

  Gaming Articles

  Contact us

  About us

 

   
Google+

Looping Threw a Recordset in ASP.NET 2.0

One thing that web programmers need to do often is work with data from a database. One great way to do this is to use a recordset. Also, for those who have a backround in classic ASP, you are used to working with recordsets and would like a way to do it in ASP.NET.

The first thing you will have to do is connect to a database. If you don't know how to do this, you can check my article Connecting to a Database.



After you have the connection to the databse established, you can now define your string for your SQL query. In this example, I will select the columns Name and ID from the table TABLE1.

    'Specify our SQL statement
    Dim strSQL As String = "SELECT [Name], [id] FROM [TABLE1]"

Next, we need to define our command object using the connection to the database we have created and the SQL query we have just built.

    'Create the Command object
    Dim objCommand As OleDbCommand
    objCommand = New OleDbCommand(strSQL, objConnection)

After we have the command built, we then need to create a data reader that we can use to loop threw the data.

    ' Set an OleDbDataReader to the command's results
    Dim objDataReader As OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.Default)

Last, but not least, we loop threw the dataset and then close the datareader after we are done with it.

    'Load Data from Data Reader to DataTable object
    While objDataReader.Read()
      'For this to compile you will have to declare iTempName, iTempID.
      'However, I used the lines below to show you how to access the data in the recordset.
      iTempName = objDataReader("Name")
      iTempID = objDataReader("ID")
    End While

    'Close the datareader/dbconnection
    objDataReader.Close()

Also, this was included in the connecting to a database article, but remember to close your connection to the database after you are finished accessing data. This is all you need to successfully access data and loop threw a recordset. Good luck and happy programming!

Remember that in some situations the only solution is .NET development outsourcing.

Related Articles:



ASP.Net Home











Web site and all contents © Copyright DominicAcito.com 2007, All rights reserved.
Free website templates