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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:29:18+00:00 2026-05-11T18:29:18+00:00

I am populating different types objects with datarows with each attrivute of the relevant

  • 0

I am populating different types objects with datarows with each attrivute of the relevant object = to the similiarly name field in the datarow.

I’d like to use a generic function to do this. How do I force the Type of the return object from the generic function. I don’t yet know what the <T> syntax actually means:
PopulateObject<T> does not return the type as I get compiler error – Cannot implicitly convert type ‘object’ to ‘JobCard’
See my code below

public JobCard AcceptJobCard(Guid jobCardGuid, Guid userGuid)
{
    try
    {
        JobCard jc= new JobCard();
        DL_ISMS.DataSets.JobCardDS.View_JobcardDataTable dtJC = BL_ISMS.Meter.JobCard_CB.FetchJobCard(jobCardGuid);
        DL_ISMS.DataSets.JobCardDS.View_JobcardRow jcRow = dtJC[0];



        DateTime dateAccept = DateTime.Now;
        bool res = BL_ISMS.Meter.JobCard_CB.UpdatePdaJobCard(userGuid, jobCardGuid, null, null, null, JobCardStatus.Accepted.GetHashCode(), null, null, null, null, "", "", "", "", "", "", null, dateAccept, null, null, null, null, null, null);

        if (res)
        {                
            jc = PopulateObject<JobCard>(jc, jcRow);

            return jc;
        }
        else
        return jc;
    }
    catch (Exception ex )
    {
        Trace.WriteException(ex);
        throw;
    }
}

private object PopulateObject<T>(object dataObj, System.Data.DataRow dataRow)
{

    Type type = dataObj.GetType();
    System.Reflection.PropertyInfo[] proplist = type.GetProperties();
    string s = "";
    foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
    {
        s += propertyitem.Name + ":" + (propertyitem.GetValue(dataObj,null)).ToString() + "\r\n";
        propertyitem.SetValue(dataObj, dataRow["propertyitem.Name"], null);
    }
    return (T)dataObj;
}

—-updated after 2nd answer—-

using this code:
private T PopulateObject(T dataObj, System.Data.DataRow dataRow)
{

    System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();

    foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
    {
        if(propertyitem.Name != "")
            try
            {
                propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("does not belong to table"))
                {
                    propertyitem.SetValue(dataObj, PopulateObject<propertyitem.GetType()>(propertyitem, dataRow), null);
                }
                else
                throw;
            } 
    }
    return dataObj;
}

I have hit another roadblock though. One of the attributes is actually another object called Customer, with 11 attributes of it’s own. I suspect there even are more nested objects still lurking.
How would I handle the populating of these nested objects, for whom there is only 1 field in the datarow?.

To handle these objects:
– I’d have to extract the ChildObj type in the parent Objdata
– call a ProcessChildObj() function to which I pass the ChildObjType, and the complete datarow
– and in ProcessChildObj() do a name match, and set that attribute?

or (as in the code above)
– Call the PopulateObject recursively. This however presents me with a problem as the compiler complains where I try to pass the obj type into the recursive call:

propertyitem.SetValue(dataObj, PopulateObject(propertyitem, dataRow), null); //causes compiler msg “Operator ‘<‘ cannot be applied to operands of type ‘method group’ and ‘System.type'”

How do I extract the type of the nested childObj to pass the type as a parameter?

  • 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-11T18:29:18+00:00Added an answer on May 11, 2026 at 6:29 pm

    Change your method’s signature to:

    private T PopulateObject<T>(T dataObj, System.Data.DataRow dataRow)
    

    Also, I think you don’t need to return any object because your only updating an existing object. You can just use:

    private void PopulateObject(object dataObj, System.Data.DataRow dataRow)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m populating data for different entities into set of lists using generic lists
Im populating a GridView from List so am forced to use TemplateField controls to
im populating the data in the combo box using dataset tables...with some table name..now
I have 3 different tabs where i am displaying data using jQGrids(each tab contain
I am using Google map in android where I am populating different markers on
I'm using jshashtable.js by Tim Down. I'm populating two different hashtables using the following
I am populating my WPF ComboBox like this foreach (Function fx in XEGFunctions.GetAll()) {
I have one package contains many Stored Procedure for populating diffrent tables. This package
Im populating viewdata with a bunch of css style properties. <span style=font-size:50pt; font-family:<%= ViewData[font]%>;><%=
i am populating a Category dropdown list with LINQ. (Northwind databse) var Category =

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.