Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:32:06+00:00 2026-06-18T04:32:06+00:00

I am working on a project using 3 tier architecture. I want to knw

  • 0

I am working on a project using 3 tier architecture.
I want to knw about how to pass datareader value from DAL to Presentation layer

My code is like this.
In DAL layer

public class HomeDAL
{
 public SqlDataReader DefaultSearchFriends(long userid)
 {
    SqlConnection SocialConn = new SqlConnection(connstr);

    using (SqlCommand comm = new SqlCommand("proc_FriendsSearch", SocialConn))
    {
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@userid", userid);
        SocialConn.Open();
        SqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
        return dr;
    }
 }
}

In BAL layer

public class HomeBAL
{
    public SqlDataReader DefaultSearchFriends(long userid)
    {
       HomeDAL HDAL = new HomeDAL();
       SqlDataReader dr = HDAL.DefaultSearchFriends(userid);
       return dr;
    }
}

On Presentaion Layer I wrote this on Page load

HomeBAL HBAL = new HomeBAL();
SqlDataReader dr = HBAL.DefaultSearchFriends(user_id);
while (dr.Read())
{ 
 //some code
}

Now i want to know two things

1- Is this right to call datareader in this way or there is some better logic.

2- how to close datareader object in BAL layer and in DAL layer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:32:07+00:00Added an answer on June 18, 2026 at 4:32 am

    Well, the basic idea of a layered architecture is to decouple the different components for several reasons.
    Some reasons are testability, maintainability, extensibility but there are many more.

    To pass the data between these layers – well it depends a bit on the kind of data – but usually you would use some simple classes as data transfer objects (DTO), which would be filled with data in the DAL. E.g.

    public class Person
    {
      public string Name {get; set;}
      public string FirstName {get; set;}
      ...
    }
    

    With your approach you are breaking this idea, because you’re passing the DataReader to presentation layer which implies, that you cannot switch the DAL technology without touching the other layers. E.g. if you want to use Entity Framework you would have to modify every part in the code, where you’re currently using the SqlDataReader.

    You can also see, if you’d stick to the idea behind the layered approach, you don’t have to think about your second question.

    I hope this helps a little bit.

    EDIT

    Ok I’m a bit curious, that you don’t find a proper solution. But anyways, the first and simplest approach could be, don’t publish the SqlDataReader. Handle its life cycle in the DAL. Means, assuming that you’re using my DTO above

    public class HomeDAL
    {
     public List<Person> DefaultSearchFriends(long userid)
     {
        SqlConnection SocialConn = new SqlConnection(connstr);
    
        using (SqlCommand comm = new SqlCommand("proc_FriendsSearch", SocialConn))
        {
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.AddWithValue("@userid", userid);
            SocialConn.Open();
            SqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
    
            var persons = new List<Person>();
    
            while (dr.Read())
              persons.Add(new Person { Name = dr["Name"], FirstName = dr["FirstName"] });
    
            dr.Close();
    
            return persons;
        }
     }
    }

    would be the much better approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The project I'm working is using n-tier architecture. Our layers are as follows: Data
I am working on a project using JavaMail. I want to access my Gmail
The project I'm working on has a bunch of service-tier unit tests using Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.
I am working on project using asp.net mvc3 C# . I want to change
i am working on project using play 2 framework with java. I want to
I'm working on a project using C++. I want a TimerHandler to be called
I'm working on a project using an Arduino and as such, I'm reading from
I'm working on important project using Flex framework and i want to keep my
I'm working on a Java project that incorporates a PostgresSQL 9.0 database tier, using
I am working on an n-tier web project, The data access layer is in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.