Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreach statement cannot operate on variables of type 'System.Data.DataSet'

I am trying to create a thumbnail for an image and display that thumbnail in a DataGrid. I did something similar before but this code is a bit different and I am getting this error on the foreach:

Error 2185 foreach statement cannot operate on variables of type 'System.Data.DataSet' because 'System.Data.DataSet' does not contain a public definition for 'GetEnumerator'

 protected void LoadVehicle(string Reg)
    {
        if (Request.QueryString["type"] == "vehicle")
        {
            int vehCount = 1;
            DataSet veh = DataUtils.GetVehicleFromReg(Company.Current.CompanyID, Reg);
            foreach (Vehicle vehicle in veh)
            {
                tabsPOD.Controls.Add(GetDivVehicle(vehCount, vehicle));
                vehCount++;
            }
        }
    }

I was trying to copy this similar code:

protected void LoadDockets(int JobID)
    {
if (Request.QueryString["type"] == "dbrief")
        {
            int DbriefCount = 1;

            List<JobPieceSerialNo> Serials = JobPieceSerialNo.GetJobPieceSerialsByJob(JobID);
            foreach (JobPieceSerialNo serNo in Serials)
            {
                tabsPOD.Controls.Add(GetDivDbrief(DbriefCount, serNo));
                DbriefCount++;
            }
        }
}

The problem is GetVehicleFromReg is saved in a file called DataUtilies but then all the properties for the vehicles is saved in a class file called Vehicles.

like image 817
user123456789 Avatar asked Sep 15 '15 13:09

user123456789


2 Answers

The problem is, that dataset represents whole data, with tables and rows. You could not iterate it. You could iterate all tables:

foreach(DataTable table in dataSet.Tables)

and than even all rows in the table:

foreach(DataRow row in table.Rows)

If i could assume that you do have only one table in your dataset, so you could write something like:

foreach(DataRow row in veh.Tables[0].Rows)

but id depens on what exact data structure your dataset has.

like image 166
Maksim Simkin Avatar answered Sep 28 '22 13:09

Maksim Simkin


A DataSet is a container of Tables. So you can enumerate myDataSet.Tables. This is a DataTableCollection.

See also MSDN: https://msdn.microsoft.com/en-us/library/system.data.dataset.tables(v=vs.110).aspx

like image 28
Roy Dictus Avatar answered Sep 28 '22 11:09

Roy Dictus