Solved

C# -- "foreach" ?

Posted on 2011-03-24
7
523 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
Technology Partners: 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

756 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