Solved

C# -- "foreach" ?

Posted on 2011-03-24
7
518 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 how to clear a vector as well as how to detect empty vectors in C++.

706 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

19 Experts available now in Live!

Get 1:1 Help Now