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

  • Home
  • SEARCH
  • 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 128309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:36:54+00:00 2026-05-11T05:36:54+00:00

Please keep in mind that I’m new to C# and OOP so I apologize

  • 0

Please keep in mind that I’m new to C# and OOP so I apologize in advance if this seems like an easy question to some. I’m going back through my code and looking for ways to objectify repetitive code and create a class for it so that I can simply reuse the class. That being said, I’m not looking to learn NHibernate or any other ORM just yet. I’m not even looking to learn LINQ. I want to hack through this to learn.

Basically I use the same bit of code to access my database and populate a drop-down list with the values that I get. An example:

   protected void LoadSchools()     {         SqlDataReader reader;          var connectionString = ConfigurationManager.ConnectionStrings['MyConnectionString'].ConnectionString;         var conn = new SqlConnection(connectionString);         var comm = new SqlCommand('SELECT * FROM [Schools] ORDER BY [SchoolName] ASC', conn);          try         {             conn.Open();             reader = comm.ExecuteReader();             cmbEditSchool.DataSource = reader;             cmbEditSchool.DataBind();               cmbEditSchool.Text = 'Please select an existing school to edit...';             if (reader != null) reader.Close();         }          finally         {             conn.Dispose();         }     } 

I use this same bit of code, over and over again throughout my program, on different pages. Most often, I’m populating a drop-down list or combo box, but sometimes I will populate a gridview, only slightly altering the query.

My question is how can I create a class that will allow me to call a stored procedure, instead of manually using queries like my example, and populate my different controls? Is it possible to do with only 1 method? I only need to start with selects. I’ve been reading up on IEnumerable which seems like the appropriate interface to use, but how do I use it?

Edited to add:

I marked Rorschach’s answer as THE answer because s/he addressed my IEnumerable question. I also understand the need for a proper DAL and perhaps BLL layer. What I was trying to get to was that. I can build a DAL using datasets and table adapters which, in the end, gets me a strongly typed dataset. However, I feel a bit removed from the code. I was after a straight-forward way of building the DAL myself starting with the code that I gave above. Perhaps I’m not wording my question, or what I’m after, correctly.

At any rate, Rorschach came closest to answering my actual question. Thanks.

  • 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. 2026-05-11T05:36:55+00:00Added an answer on May 11, 2026 at 5:36 am

    You can create a class that lets you call stored procedures (this is known as a Data Access Component (DAC) class, which is usually referenced by a Business Component (BC) class, but it is outside the scope of your question).

    There are a few objects you will want to use in this new class: Microsoft.Practices.EnterpriseLibrary.Data.Database Microsoft.EnterpriseLibrary.Data.DatabaseFactory System.Data.Common.DBCommand

    The DAC class will look similar to what you have:

    public class DataAccess {     public DataAccess()     {     }     public System.Collections.IEnumerable GetSchoolData()     {         string connectionString = ConfigurationManager.ConnectionStrings['MyConnectionString'].ConnectionString;         Database db = DatabaseFactory.CreateDatabase(connectionString);         string sqlCommand = 'GetSchoolData';         DbCommand comm = db.GetStoredProcCommand(sqlCommand);         //db.AddInParameter(comm, 'SchoolId', DbType.Int32); // this is in case you want to add parameters to your stored procedure          return db.ExecuteDataSet(comm);     } } 

    And your page code will look like this:

    public class SchoolPage : Page {   public void Page_Init(object sender, EventArgs e)   {     DataAccess dac = new  DataAccess();     cmbEditSchool.DataSource = dac.GetSchoolData();     cmbEditSchool.DataBind();   } } 

    Note that this is just to help you learn how to do this. It is not a good approach to development because you are opening up your Data Access Layer to the outside world (which is bad).

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

Sidebar

Related Questions

Please keep in mind that I am new to world of .Net and F#.
Please suggest some good resources to start writing Java Web services.
Please read the whole question. I'm not looking for an approach to managing multi-lingual
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Let's say that I have a system that has some pages that are public
There are some rumors floating around that the team at my company will soon
I have a program in access that is using some linked ODBC tables. I
Please give me the direction of the best guidance on the Entity Framework.
Please bear with me, I'm just learning C++. I'm trying to write my header
Please explain to me why the very last echo statement is blank? I expect

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.