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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:36:05+00:00 2026-05-23T23:36:05+00:00

I have a question on generics. I am trying to convert my code using

  • 0

I have a question on generics. I am trying to convert my code using generic so I don’t need to change my code again for every specific type like CompanyBO, EmployeeBO etc…

Here is my code:

public List<T1> GetAllEmployees<T1>()
{
    try
    {
        var objEmployeeList = new List<T1>();

        SqlConnection objSqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ConnectionString);

        if (objSqlCon.State == System.Data.ConnectionState.Closed)
        {
            objSqlCon.Open();
        }

        SqlCommand objSqlCmd = new SqlCommand("SELECT EmployeeID, ContactID, LoginID, Title FROM HumanResources.Employee", objSqlCon);

        SqlDataReader objSqlDataRead;

        objSqlDataRead = objSqlCmd.ExecuteReader();

        while (objSqlDataRead.Read())
        {
            objEmployeeList.Add(FillDataRecord(objSqlDataRead));
        }

        return objEmployeeList;
    }
    catch (Exception ex)
    {

        throw ex;
    }
}


private EmployeeBO FillDataRecord(IDataRecord objDataRecord)
{
    try
    {
        EmployeeBO objEmployeeBO = new EmployeeBO();

        if (!objDataRecord.IsDBNull(objDataRecord.GetOrdinal("EmployeeID")))
        {
            objEmployeeBO.EmployeeID = objDataRecord.GetInt32(objDataRecord.GetOrdinal("EmployeeID"));
        }

        if (!objDataRecord.IsDBNull(objDataRecord.GetOrdinal("ContactID")))
        {
            objEmployeeBO.ContactID = objDataRecord.GetInt32(objDataRecord.GetOrdinal("ContactID"));
        }

        if (!objDataRecord.IsDBNull(objDataRecord.GetOrdinal("Title")))
        {
            objEmployeeBO.Title = objDataRecord.GetString(objDataRecord.GetOrdinal("Title"));
        }

        if (!objDataRecord.IsDBNull(objDataRecord.GetOrdinal("LoginID")))
        {
            objEmployeeBO.LoginID = objDataRecord.GetString(objDataRecord.GetOrdinal("LoginID"));
        }

        return objEmployeeBO;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

I am getting 1 error : cannot convert from ‘EmployeeBO’ to ‘T1’

Can any please help me to create this Generic Method…

  • 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-23T23:36:06+00:00Added an answer on May 23, 2026 at 11:36 pm

    Your FillDataRecord can only ever return an EmployeeBO, so it’s not clear why you want to use generics in the first place. Why would a name called GetAllEmployees want to return a List<CompanyBO> for example?

    It looks like you should just change the signature to:

    public List<EmployeeBO> GetAllEmployees()
    

    and make the corresponding change within the method.

    If you want to write a generic method, you should work out what would change between this method and the corresponding method for companies. I would suggest that in the current design, it would be:

    • The SQL statement
    • The method to use to convert an IDataRecord into an entity

    So that would suggest you have something like:

    public List<T> FetchEntities<T>(string sql, Func<IDataRecord, T> converter)
    

    You’d then specify the converter using a method group conversion, e.g.

    const string FetchAllEmployeesSql = "SELECT ...";
    
    List<EmployeeBO> employees = FetchEntities(FetchAllEmployeesSql,
                                               FillEmployeeDataRecord);
    

    Is there any reason you want to build your own sort of ORM for this rather than using some of the many existing ones, by the way?

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

Sidebar

Related Questions

This is mainly a theoretical question i.e I don't have any need for a
No, this is not a question about generics. I have a Factory pattern with
We have a question with regards to XML-sig and need detail about the optional
I have a question about using streams in .NET to load files from disk.
I have a question regarding the following code: abstract class a { public static
Okay the question title may not have made sense... mostly because I don't know
This is what I have so far using System; using System.Collections.Generic; using System.Data.Linq; using
I have a question about the following exception I received trying to complete a
I am working on a project, and I have a generic abstract type that
How can I make a generic helper method to convert the type used by

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.