?
Solved

C# -- "foreach" ?

Posted on 2011-03-24
7
Medium Priority
?
526 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 1400 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 600 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

Independent Software Vendors: 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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

777 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