I am working on a small piece of code to loop through my database data. I get to rows from the database and want loop through one with an foreach. Then within the foreach, I am currently trying to make a console message so I can see if it works.
The function Common.Log
is a function made by myself and a co-worker.
This is the code:
SqlParameter bonIdParam = new SqlParameter("bonId", bonId);
DataTable controleTable = RidderQuery(controleQuery, bonIdParam);
DataRow controleRow = controleTable.Rows[0];
int code= controleRow .Field<int>("CODE");
int productionDone = queryRow.Field<int>("PRODUCTIONDONE"); // 1 = true, 0 = false
foreach(int checkedCode in controleRow .Field<int>("CODE"))
{
Common.Log(checkedCode.ToString);
}
The problem that I have is, that the foreach loop does not work. yes I know that very unclear, but I don't know how to explain it better. So these are the errors im getting:
Error 3: Argument 1: cannot convert from 'method group' to 'string'
Error 1: foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'
Error 2: The best overloaded method match for 'LocatieVerwerkingWifi.Common.Log(string)' has some invalid arguments
What can I do to fix these errors? I think the problem is in this line:
foreach(int checkedCode in queryRow.Field<int>("CODE"))
But I don't understand what is going wrong. Can someone explain/help me with this issue?
controleRow
is a DataRow
and controleRow.Field<int>("CODE")
returns a single int-field which you can't enumerate. checkedCode.ToString
must be checkedCode.ToString()
. Maybe you want to loop all rows:
foreach(DataRow row in controleTable.Rows)
{
int checkedCode = row.Field<int>("CODE");
Common.Log(checkedCode.ToString());
}
You could also create a single string from all codes with LINQ and String.Join
:
var allCodes = controleTable.AsEnumerable().Select(r => r.Field<int>("CODE"));
Common.Log(string.Join(",", allCodes));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.