Solved

C# -- "foreach" ?

Posted on 2011-03-24
7
521 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
7 Comments
 
LVL 142

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 142

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 142

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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 …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now