Solved

C# -- "foreach" ?

Posted on 2011-03-24
7
524 Views
Last Modified: 2013-12-17
The first "foreach" below works.

What do I need to put in the below
"what here ??" section so the
second "foreach" works ?
---------------------------------------------------
DataSet ds = new DataSet("Temp");
adapter.Fill(ds);

// Loop through the data table and change names like "Doe,John" to "John Doe"
foreach (DataRow row in ds.Tables[0].Rows)
    row[0] = Regex.Replace((string)row[0], @"([^,]*),([^,]*)", "$2 $1");

// Loop through the data table and change blanks to "0"
foreach (DataRow row in ds.Tables[0].Rows)
    row[5] = what here ??;

return ds;
0
Comment
Question by:finance_teacher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35206164
what about:

foreach (DataRow row in ds.Tables[0].Rows)
{  if ( row[5] == "" ) { row[5] = "0"; }
} 

Open in new window

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 350 total points
ID: 35206167
this said, I would avoid the second loop:
foreach (DataRow row in ds.Tables[0].Rows)
{
   row[0] = Regex.Replace((string)row[0], @"([^,]*),([^,]*)", "$2 $1");
   if ( row[5] == "" ) { row[5] = "0"; }
}

Open in new window

0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35206272
This should also work:
foreach (DataRow row in ds.Tables[0].Rows)
{
   row[0] = Regex.Replace((string)row[0], @"([^,]*),([^,]*)", "$2 $1");
   row[5] = row[5].Replace("","0");
} 
//Or if you are worried that row[5] may be null then
foreach (DataRow row in ds.Tables[0].Rows)
{
   row[0] = Regex.Replace((string)row[0], @"([^,]*),([^,]*)", "$2 $1");
   if(row[5] == null)
       row[5] = "0";
   else
       row[5] = row[5].Replace("","0");
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35206392
>row[5].Replace("","0");

I don't think that that will really work ?!

0
 
LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 150 total points
ID: 35213851
Try this:

Change this line:
    row[5] = what here ??;

like this:
    row[5] = String.IsNullOrEmpty(row[5]) ? "0" : row[5] ;
0
 

Author Comment

by:finance_teacher
ID: 35391861
I will test.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question