Solved

Dynamic list creation

Posted on 2009-04-14
14
228 Views
Last Modified: 2013-12-17
Greetings all

I have a method that returns the results of stored procedure, selected by the user. I would like to return that data to the UI layer via a generic list. The problem is that I have no way of knowing how many fields or what type comes back.

Is this possible using a List/Class in a similiar way to the attached snippet (this is just an example of a simple case) ??


In advance, thanks!!!

    allanamrk
SAMPLE CLASS

 

List<EMailAddress> emailContacts = new List<EMailAddress>();

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DataTransferObjects

{

    public class EMailAddress

    {

 

        private string name;

        private string email;

        

        public EMailAddress()

        {

 

        }

 

        public EMailAddress(string pName, string pEmail)

        {

            name = pName;

            email = pEmail;

        }

 

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

 

        public string Email

        {

            get { return email; }

            set { email = value; }

        }

 

    }

}

Open in new window

0
Comment
Question by:allanmark
  • 6
  • 6
14 Comments
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
What about returning a list of DataRows?

List<DataRow> MyDataRowList = new List<DataRow>();

Dabas
0
 

Author Comment

by:allanmark
Comment Utility
I tried that, but I need to work with the data in a GridView. When I linked the List (of DataRow) to the grid, it complained that there was no DATAFIELD - what I had was a list of array.
0
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
Show me the code where you bind to the grid
0
 

Author Comment

by:allanmark
Comment Utility
queryResults =
    BusinessLogic.Business.GetQueryResults(.......);

// Binding
gvResults.DataSource = queryResults;
 gvResults.DataBind();
0
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
And the code for GetQueryResults?
0
 

Author Comment

by:allanmark
Comment Utility
Ooops!!!

Sorry!  I'm having a really blonde day!! (no offense intended, if you happen to be blonde!!!).


 public static List<DataRow> GetQueryResults(string procName, List<DbParam> dbParms, string [] fieldNames)

        {

            List<DataRow> queryResults = new List<DataRow>();
 

            queryResults = DataAccess.ReturnQueryResults(procName, dbParms);
 

            return queryResults;

        }
 
 

public static List<DataRow> ReturnQueryResults(string procName, List<DbParam> dbParms)

{

            List<DataRow> returnedRecords = new List<DataRow>();

                        

            SqlParameter workParam;
 

            SqlConnection sqlConn = new SqlConnection(connectStr);
 

            DataSet dsQuery = new DataSet();

                   

            SqlDataAdapter daQuery = new SqlDataAdapter(procName, sqlConn);

            daQuery.SelectCommand.CommandType = CommandType.StoredProcedure;
 

            if (dbParms.Count > 0)

            {

                foreach (DbParam parm in dbParms)

                {

                    workParam = GetParameter(parm.ParmName, parm.ParmType, parm.ParmVal);

                    daQuery.SelectCommand.Parameters.Add(workParam);

                }

            }

         

            daQuery.Fill(dsQuery, "QueryResults");
 

            List <DataRow> returnedRecords = new List<DataRow>(dsQuery.Tables["QueryResults"].Select());
 

            return returnedRecords;

}

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 27

Accepted Solution

by:
Dabas earned 300 total points
Comment Utility
Thanks

I suggest that you try filling a DataTable instead of a DataSet
Then instead of returning a List of rows, return the DataTable.

DataGridViews work very well with DataTables

Dabas
0
 

Author Comment

by:allanmark
Comment Utility
COuld do that.
It's just that we have been encouraged to work with Lists, so as to achieve independance/separattion between the DataLayer and the UI and also to n=minimise reliance on System.Data.

0
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
It could be the right approach, except for your statement 'The problem is that I have no way of knowing how many fields or what type comes back'

I do not see anything wrong with reliance on System.Data. The same can be argued against reliance on System.Collections.

Dabas
0
 

Author Comment

by:allanmark
Comment Utility
Ok.

So maybe what I need to do is work with the lists, where I have a definite structure that is returned and where it is something flexible / unknown (shoudl only be this one case) return a datatable.

Does that sound like an acceptable "work-around" compromsie, to you?
0
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
Yes
0
 

Author Closing Comment

by:allanmark
Comment Utility
Many thanks!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now