Net-informations.com
SiteMap  | About    

ASP.NET DataReader

DataReader Object in ADO.NET is a stream-based , forward-only, read-only retrieval of query results from the Data Sources , which do not update the data. The DataReader cannot be created directly from code, they can created only by calling the ExecuteReader method of a Command Object.

After creating an instance of the Command object, you have to create a DataReader by calling Command.ExecuteReader to retrieve rows from a data source.

vb.net

  Dim reader As SqlDataReader = cmd.ExecuteReader

C#

  SqlDataReader reader = cmd.ExecuteReader();

When the ExecuteReader method in the SqlCommand Object execute , it will instantiate a SqlClient.SqlDataReader Object. When we started to read from a DataReader it should always be open and positioned prior to the first record. The Read() method in the DataReader is used to read the rows from DataReader and it always moves forward to a new valid row, if any row exist . You should always call the Close method when you have finished using the DataReader object.

The following ASP.NET program execute sql statement and read the data using SqlDataReader.

Default.aspx

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
		<br />
		<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
		<br />
		<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
	</div>
	</form>
</body>
</html>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.cs
default.aspx.vb






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.