Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Implementing MARS using Visual Studio 2005

Author: DevASP Team
Download Source Code :

In this article I will try to explain you how you can implement the new concept MARS (Multiple Active Result Sets) using two SqlDataReaders objects.

MARS (Multiple Active Result Set) is the new concept that is introduce in Visual Studio 2005. MARS means you can work with same reader without closing the already opened reader. This provides an ease to developers that to keep track of objects that need to be closed before using same object.


The key point in using the MARS is to set the Multiple Active Result set parameter to True like as mentioned MultipleActiveResultSets=True;. You will set this parameter in your connection string else it will give you error that reader is already opend. Finally the connection you will form is as follows:


Dim objConn As New SqlConnection("MultipleActiveResultSets=True;Persist Security Info=False;User ID=sa;Password=pwd;Initial Catalog=tempdatabase")

Add two controls label control and grid view control on your .aspx page and on the page load event add the following code:

    Dim objConn As New SqlConnection("MultipleActiveResultSets=True;Persist Security Info=False;User ID=sa;Password=master;Initial Catalog=tempdatabase")

    Dim objReader As SqlDataReader

    Dim objCmd As New SqlCommand



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load




        Label1.Text = "Binding Two Reader without closing already opened Reader Object"


        objCmd = New SqlCommand("proc_test", objConn)

        objCmd.CommandType = CommandType.StoredProcedure

        objReader = objCmd.ExecuteReader()


        While (objReader.Read)

            objCmd = New SqlCommand("Select * from CargoTicket Where Owner='" & objReader(1).ToString & "'", objConn)

            objReader = objCmd.ExecuteReader


            GridView2.DataSource = objReader


        End While


    End Sub

Remember don’t forget to import Sql.Data and Sql.Data.SqlClient in your application. Note the sample application is also attached.

Article Comments
topic is too good,
it will support so much
to the developer far as my experience is concern ....

Posted on 5/15/2007 3:14:55 AM by abhishek

Add Article Comment:
Name :
Email Address :
Comments :
<< Working with Array of Controls in C#

Disclaimer - Privacy
© 2002-2018