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 3323792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:20:40+00:00 2026-05-17T23:20:40+00:00

Good Morning, I have created my first generic method pieced together from a bunch

  • 0

Good Morning, I have created my first generic method pieced together from a bunch of Google Searches. I would like for someone to look this over and let me know if I am breaking any major rules or if there are ways to improve this method.

The method calls a stored procedure in sql and then uses reflection to assign the properties based on the values read from the DataReader schema. The stored procedures are coded so that they return the exact property names expected by the classes. Here is the code:

       public static List<T> GetList<T>(string SQLServer, string DBName,
        string ProcedureName, Dictionary<string, string> Parameters )
         where T : new()
    {
        List<T> list = new List<T>();

        //Setup connection to SQL
        SqlConnection SqlConn = new SqlConnection(ConnectionString(SQLServer, DBName));
        SqlCommand SqlCmd = new SqlCommand(ProcedureName, SqlConn);
        SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
        SqlDataReader reader;

        //Process Parameters if there are any
        foreach (KeyValuePair<string, string> param in Parameters)
        {
            SqlCmd.Parameters.AddWithValue(param.Key, param.Value);
        }

        SqlConn.Open();
        reader = SqlCmd.ExecuteReader();

        //Get The Schema from the Reader
        //The stored procedure has code to return
        //the exact names expected by the properties of T
        DataTable schemaTable = reader.GetSchemaTable();
        List<string> fields = new List<string>();
        foreach (DataRow r in schemaTable.Rows)
        {
            fields.Add(r[0].ToString());
        }


        while (reader.Read())
        {
            T record = new T();

            foreach (string field in fields)
            {
                //Assign the properties using reflection
                record.GetType().GetProperty(field).SetValue(
                    record, reader[field],
                    System.Reflection.BindingFlags.Default,
                    null,null,null);
            }

            list.Add(record);
        }
        return list;
    }
  • 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-05-17T23:20:41+00:00Added an answer on May 17, 2026 at 11:20 pm

    I don’t know if that’s something I would ever do or not. I would probably use an ORM first, e.g. Entity Framework. I have done stuff similar to that in the past, though, and there are some draw-backs:

    • Reflection can be slower than just specifying everything, how much depends, and it might not be an issue for you at all.
    • The bigger issue for me is simply that you risk more runtime errors rather than compile-time errors, so saves time up front, but may introduce annoying bugs in the long run.

    About the only thing I would definitely say to do is to make sure to use try/catch/finally, try/finally, or wrap SqlConnection, SqlCommand, and SqlDataReader within using()s. I just spent 2 days refactoring because previous developers didn’t close any connection or datareader, and the connection pool was blowing up and refusing connections.

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

Sidebar

Related Questions

Good morning. I have an XML file which contains lists of warning and errors
Good morgning, I would like to be able to analyze text where I am
Good morning, I am about to start writing an Excel add-in for Excel 2002.
Good morning, I am the developer of a medium sized PDA application that will
Good morning, I work in a small shop (only two of us) and we
Good morning, I am working on a C# winform application that is using validation
Good morning, Apologies for the newbie question. I'm just getting started with ASP.NET internationalization
Good morning, afternoon, evening or night (depending on your timezone). This is just a
Good afternoon, This should be an easy one. I've done the cookie-cutter default ASP.NET
Good evening I've got a little problem with my DataGridView in a .NET Windows

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.