Immediate window in C# VS2005 - how to loop through collection

I've got an error during loading data into DataTable - "Constraint violation...Relax constraints.". I need to loop in immediate window to find DataRow that HasError. I tried:

foreach(DataRow dr in dtab) if(dr.HasErrors) Console.WriteLine(dr["ID"]);

but it failed: Invalid expression term 'foreach'.

Is there any way to achive it

Billberry



Answer this question

Immediate window in C# VS2005 - how to loop through collection

  • Zadoras

    Here's what I get from MSDN;

    The Immediate window is used to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging.

    So, immediate window does not allow the usage of looping constructs in it and foreach is one of them. Thats why immediate window is called IMMEDIATE.

    It helps only to run simple statements not using memory stack. If you really wanna debug via looping then do it in your actual code and you may remove it when you are done debugging.



  • Robbie Page


    Have you tried "dTab.Rows;" in the immediate windows

    Has dTab.Rows is a collection it should show you all the rows.

    K.

  • mcgin1591

    Did read your initial question, and it seems you want to find rows with error in datatable.

    If so, use DataTable.GetErrors() method which returns an arrays with rows having errors.



  • Evgeny Popov

  • TCSC

    It has nothing to do with my problem. There's no easy and fast way to iterate in Watch through DataTable.Rows collection to find the specific row.

    How can I find DataRow in DataTable that has HasError = true among thousand records
    You can loop through collection in VB.NET in Immediate Window with 'For Each' command. How to do it in C# or how to switch into VB.NET in Immediate

    Billberry


  • adorer

    You can I think, but only in VB, not C#.

    See: http://www.devx.com/tips/Tip/15231 type=kbArticle&trk=MSCP

    Bilberry


  • Fox3

    Try 'Quick Watch' Window i.e. select your collection variables and press Ctrl+Alt+Q.

  • Vaish

    It doesn't work. Immediate window tells the same:

    foreach(DataRow dr in dtab.Rows) if(dr.HasErrors) Console.WriteLine(dr["ID"]);
    Invalid expression term 'foreach'
    foreach(DataRow dr in dtab.Rows) {if(dr.HasErrors) Console.WriteLine(dr["ID"]);}
    Invalid expression term 'foreach'

    It seems, that there's no such a possibility to use 'foreach' in Immediate window.

    It's very bad and there's no info about it in any other MS white (or any other color) paper.

    Billberry


  • Ido Ran

    don't loop through the rows of the table looking for the errors - loop through the error rows only

    Get the error rows.

    DataRow[] theRows = dt.GetErrors();

    //need someplace to store the error

    dt.Columns.Add("ErrorDesc", Type.GetType("System.String"));

    if (dt.HasErrors)

    {

    //add the description column to the dataset

    DataRow[] theRows = dt.GetErrors();

    //some debugging code

    //Console.WriteLine("Number of error Rows: " + theRows.Length.ToString() + ".... Hit Enter");

    //Console.ReadLine();

    foreach (DataRow theRow in theRows)

    {

    if (theRow.HasErrors)

    {

    theRow.BeginEdit();

    //update the stage table's errorDesc column with row error

    theRow["ErrorDesc"] = theRow.RowError;

    theRow.EndEdit();

    }

    }



  • daat99

    I asume you just want to foreach through the rows.

    Your initial lines of code was not perfect.

    foreach(DataRow dr in dtab) if(dr.HasErrors) Console.WriteLine(dr["ID"]);

    It should be:

    foreach(DataRow dr in dtab.Rows)
    {
    if(dr.HasErrors) Console.WriteLine(dr["ID"]);
    }

    Hope this helps.

    Marcel


  • gafferuk

    MihaiBejenariu wrote:

    Use conditional breakpoints.

    http://msdn2.microsoft.com/en-gb/library/7sye83ce.aspx

    It assumes that I modify the source code I debug and go further (Edit and Continue). I'd like not to do it, and sometimes it is even not possible, e.g. in catch clause it cannot be done.

    But it is a kind of workaround, that makes possible to find what I need. Thx.

    Is there anybody (any MVP or any other MSgeek) that knows, whether it is possible in VS2005 in C# in Immediate window to use 'for' or 'foreach'.

    Hopefully, Billberry


  • Mystagogue

    That's true, but still I need to modify code, and I can't look for a property in debug mode in a big amount of data.

    I did a workaround with Edit & Continue, but I can't find any info whether 'foreach' does or doesn't work in Immediate window.

    Don't tell me what can I done, but tell me what is the right syntax in Immediate widnow for 'for' clause.

    Bilberry


  • Immediate window in C# VS2005 - how to loop through collection