I am looping through a dataset for each data row
foreach (DataRow DRow in ds.Tables[0].Rows)
I would like to jump the current row when ever a if statement is true.
Any clue how to do it?
Thanks in advance !
Use continue:
foreach (DataRow DRow in ds.Tables[0].Rows)
{
if(expression)
continue;
}
continue skips the remaining part of the foreach block for the current element an continues at the next new element in your collection.
Try this:
foreach (DataRow DRow in ds.Tables[0].Rows)
{
if(true) // escape condition met
continue;
}
You are looking for the continue keyword...
foreach (DataRow DRow in ds.Tables[0].Rows) {
if(condition)
continue;
}
The continue; instruction tells a loop to skip the rest of the code and move to the next iteration.
foreach (DataRow DRow in ds.Tables[0].Rows)
{
if (--condition here--) continue;
}
Related
string field = ViewState["Field"].ToString();
DataTable dt = (DataTable)Session["Academic"];
foreach (DataRow dr in dt.Rows)
{
if (dr["Degree"].ToString() == field)
{
dr.Delete();
dt.AcceptChanges();
}
}
Session["Academic"] = dt;
gdvwAcademic1.DataSource = Session["Academic"] as DataTable;
gdvwAcademic1.DataBind();
when this code executed raise error as "collection was modified enumeration operation might not execute." why this so..?
You cannot modify a collection in a foreach. Try this as an alternative to apomene's answer (Pretty much does the same thing, except using the remove method of a list instead of indexes.
List<DataRow> toDelete = new List<DataRow>();
foreach(DataRow dr in dt.Rows){
if(dr["Degree"].ToString() == field){
toDelete.Add(dr);
}
}
foreach (DataRow dr in toDelete){
dt.Rows.Remove(dr);
}
This should solve your problem.
You can't modify the collection in a foreach. So don't delete or add things in it.
You could prevent this error by creating a new collection with items you want to modify, for example with LINQ (the ToList() is essential):
var toDelete = dt.AsEnumerable().Where(r => r["Degree"].ToString() == field).ToList();
Now you can loop this without a problem.
foreach(DataRow rowToDelete in toDelete)
rowToDelete.Delete();
dt.AcceptChanges(); // you could do that also in the loop now
you cant delete inside foreach use for loop or make a delete List:
List <int> toBeDel=new List<int>();
foreach (DataRow dr in dt.Rows)
{
if (dr["Degree"].ToString() == field)
{
toBEDel.Add(indexOf(dr));
}
}
foreach (int i in toBeDel)
{
dt.Rows[i].Delete();
}
for (int i = 0;i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (dr["Degree"].ToString() == field)
{
dr.Delete();
i-=1;
}
}
dt.AcceptChanges();
you can use this, one loop.
For any people find this post.
I have created 3 datatables
var dt1= new DataTable();
var dt2= new DataTable();
var dt3= new DataTable();
Then i loop
foreach (DataRow row1 in dt1.Rows)
{
dt3.Rows.Add(row1.ItemArray);
foreach (DataRow row2 in dt2.Rows)
{
var Id2 = row1["Id"];
var Id1= row2["Id"];
if (Id1 == Id2)
{
dt3.rows["Name"] = "" ; // doesnt work
}
}
}
As you can see that i loop on 2 datatables. Then in inner loop i check if the records matches. Now if the record matches then i want to update column "Name" on dt3 datatable.
I tried using
dt3.rows["Name"] = "" ;
But this doesnt work. I know the reason that its because i again need to loop on dt3 datatable and
assign values to column in that loop. But not sure how to do it and if there is even more better solution. I mean we can find id in dt3 datatable and then update value. But not sure how to do it
Is there more intelligent solution than looping on 2 table?
The first foreach is not needed. A simple DataTable.Copy will bring all the data and structure from the original table in the destination table. Then looping on the second table and Select on the third to find the matching rows and clear the name.
dt3 = dt1.Copy();
foreach (DataRow row2 in dt2.Rows)
{
DataRow[] match = dt3.Select("ID=" + row2["ID"].ToString());
if(match.Lenght > 0)
match[0]["Name"] = "" ;
}
Not sure if this is more performant from the other answers. Need to be tested
dt3.rows doesn't work, you want to change the name of the row that you have added now. This should work:
foreach (DataRow row1 in dt1.Rows)
{
DataRow newRow = dt3.Rows.Add(row1.ItemArray);
foreach (DataRow row2 in dt2.Rows)
{
var Id2 = row1["Id"];
var Id1 = row2["Id"];
if (Id1 == Id2)
{
newRow["Name"] = "new Name";
}
}
}
Try this:
foreach (DataRow row1 in dt1.Rows)
{
var row = dt3.Rows.Add(row1.ItemArray);
foreach (DataRow row2 in dt2.Rows)
{
var Id2 = row1["Id"];
var Id1= row2["Id"];
if (Id1 == Id2)
{
row["Name"] = ""; //maybe works
}
}
}
How about,
dt2Lookup = new HashSet(
dt2.AsEnumerable().Select(row => row.Field<int>("Id")));
dt3 = dt1.Clone();
forreach (var row In dt1.AsEnumerable())
{
var newRow = dt3.Rows.Add(row.ItemArray)
if (dt2lookup.Contains(row.Field<int>("Id"))
{
newRow.SetField("Name", string.Empty);
}
}
The HashSet should provide good lookup performance.
dt3 = dt1.Copy();
var RowDictionary = dt3.Rows.OfType<DataRow>().ToDictionary(dr => dr["ID"].ToString());
//replace by Dictionary<string,List<DataRow>> in case ID is not unique and fill it with a foreach loop.
foreach (DataRow row2 in dt2.Rows)
{
DataRow Match;
if (c.TryGetValue(row2["ID"].ToString(), out Match))
{
Match["Name"] = "";
}
}
You should call:
dt3.rows[dt3.rows.Count - 1].Columns["Name"] = "" ;
I'm trying this but I'm not sure how to proceed. Can you give me a hand?
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
DataTable table = instance.GetDataSources();
foreach (var row in table.Rows)
{
foreach (var column in table.Columns)
{
ddlPublisherServer.Items.Add(???);
}
}
Where ??? = SQL Server Name.
How would I go about extracting the sql server name from the table?
I'm working in ASP .NET with C#.
Well, if wanted to see all available information, you can iterate over the columns available:
DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
{
Console.WriteLine("{0,-15}: {1}", col.ColumnName, dr[col]);
}
Console.WriteLine();
}
Which would show something like:
ServerName : COMPUTER1
InstanceName : SQLEXPRESS
IsClustered : No
Version : 9.00.4035.00
So, to answer your question:
ddlPublisherServer.Items.Add(row[table.Columns["ServerName"]]);
Full Code:
DataTable table = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow server in table.Rows)
{
ddlPublisherServer.Items.Add(server[table.Columns["ServerName"]].ToString());
}
Well. I have a DataTable with multiple columns and multiple rows.
I want to loop through the DataTable dynamically basically the output should look as follows excluding the braces :
Name (DataColumn)
Tom (DataRow)
Peter (DataRow)
Surname (DataColumn)
Smith (DataRow)
Brown (DataRow)
foreach (DataColumn col in rightsTable.Columns)
{
foreach (DataRow row in rightsTable.Rows)
{
//output
}
}
I typed that out and noticed this would not work. Can someone please advice on a better way of doing this?
foreach (DataColumn col in rightsTable.Columns)
{
foreach (DataRow row in rightsTable.Rows)
{
Console.WriteLine(row[col.ColumnName].ToString());
}
}
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.WriteLine(row[col]);
}
Please try the following code below:
//Here I am using a reader object to fetch data from database, along with sqlcommand onject (cmd).
//Once the data is loaded to the Datatable object (datatable) you can loop through it using the datatable.rows.count prop.
using (reader = cmd.ExecuteReader())
{
// Load the Data table object
dataTable.Load(reader);
if (dataTable.Rows.Count > 0)
{
DataColumn col = dataTable.Columns["YourColumnName"];
foreach (DataRow row in dataTable.Rows)
{
strJsonData = row[col].ToString();
}
}
}
If you want to change the contents of each and every cell in a datatable then we need to Create another Datatable and bind it as follows using "Import Row". If we don't create another table it will throw an Exception saying "Collection was Modified".
Consider the following code.
//New Datatable created which will have updated cells
DataTable dtUpdated = new DataTable();
//This gives similar schema to the new datatable
dtUpdated = dtReports.Clone();
foreach (DataRow row in dtReports.Rows)
{
for (int i = 0; i < dtReports.Columns.Count; i++)
{
string oldVal = row[i].ToString();
string newVal = "{"+oldVal;
row[i] = newVal;
}
dtUpdated.ImportRow(row);
}
This will have all the cells preceding with Paranthesis({)
I am using a DataTable for some calculations in my app. I need to do the iterate trough all the rows except the first one. Is it possible?
Something like:
DataTable dt;
foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/)
{
//do something...
}
LINQ is your friend:
DataTable dt;
foreach (DataRow r in dt.Rows.Cast<DataRow>().Skip(1))
{
//do something...
}
The call to Cast() is required here since DataTable.Rows implements the non-generic IEnumerable, and linq's extension methods are only available for IEnumerable<T>
You also have another option:
DataTable dt;
foreach (DataRow r in dt.AsEnumerable().Skip(1))
{
//do something...
}
Ok you got your answers but in case you donT want to use linq. Check the index of the row in the table:
foreach (DataRow row in m_dtMatrix.Rows)
{
if (m_dtMatrix.Rows.IndexOf(row) != 0)
{
...
}
}
Here's a quick and dirty
DataTable dt;
bool isFirst = true;
foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/)
{
if( isFirst ) {
isFirst = false;
continue;
}
//do something...
}