We help IT Professionals succeed at work.
Get Started

How can i convert  the contents of a list into a dataset and make it available anywhere in my application

Olukayode Oluwole
Last Modified: 2019-11-27
In my c# application I need to read a set of data  from a table "stateorigin"
and get it ready as a dataset  for preparing a crystal report

I have 2 scripts

My first script defines the  fields in the dataset ( see script below)

  //Creates a new dataset, 2 datatables and adds data to them
   _dsData = new DataSet("TestDataSet");

  private void CreateDataSet2()
            //Creates a new dataset, 2 datatables and adds data to them
            _dsData = new DataSet("TestDataSet");

            //byte[] arrBytes;
            //System.IO.MemoryStream ms;

            using (DataTable dt = new DataTable("stateorigin"))
                dt.Columns.Add("stateid", typeof(int));
                dt.Columns.Add("statedescription", typeof(string));


Open in new window

So basically I want to read 2 fields  stateid and statedescription  from stateorigin table

Already during the creation of my application i have  a script that get a list of these same fields BUT

into a list.  ( See  script below )

[public List<StateOriginModel> GetStateCode_All()
             List<StateOriginModel> results = new List<StateOriginModel>();

            using (var conn = new NpgsqlConnection(pgrstring))
                using (NpgsqlCommand command = new NpgsqlCommand("public.spstateorigincode_getall", conn))
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(new NpgsqlParameter("countrycodex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticcountry.ToUpper() });

                    var reader = command.ExecuteReader();
                    if (reader.HasRows)
                        while (reader.Read())
                            StateOriginModel model = new StateOriginModel();

                            if ((string)reader["StateID"] != null)
                                model.Id = (int)reader["id"];
                                model.StateID = (string)reader["StateID"];  
                                model.StateDescription = (string)reader["StateDescription"];  
            return results;

Using the debugger i know that the List is populated  into a list called results

My questions are

1. Is there a way for me to  assign the contents of the list results to the dataset    _dsData

2. I need to make the contents of _dsData   available to the calling script 1
How do i define _dsData as a static class so that  i can get its data  anywhere in my application


Watch Question
Senior .Net Consultant
Top Expert 2016
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE