0

I have a foreach loop going that is adding e-mail address to an array. At the end I join the array and push it into a string.

I have an issue when someone in the database has a blank email address it messes up my logic. Can someone help me fix this?

TestGuy1: 
TestGuy2: [email protected]
TestGuy3: [email protected]

With the above information it creates a 3 length array and then turns it into a string like so:

sEmailList  "[email protected],[email protected],"  string

Code:

DataTable GetUserReportsToChain = objEmployee.GetUserReportsToChain(LoginID, ConfigurationManager.AppSettings.Get("Connection").ToString());

int RowCount = GetUserReportsToChain.Rows.Count;
string[] aEmailList = new string[RowCount];
int iCounter = 0;
foreach (DataRow rRow in GetUserReportsToChain.Rows)
{
    if (rRow["usrEmailAddress"].ToString() != "")
    {
        aEmailList[iCounter] = rRow["usrEmailAddress"].ToString();
        iCounter++;
        //String email = rRow["usrEmailAddress"].ToString(); 
    }
}

string sEmailList = String.Join(",", aEmailList);

Any idea how I can fix this when the database has a blank value for e-mail?

2
  • Please provide "expected" value. Also I see nothing particularly wrong about your string - just make code that uses it resilient to empty elements. Commented Aug 28, 2012 at 19:01
  • When rRow["usrEmailAddress"].ToString()=="" is true then there are empty slots in aEmailList not set to an email address. Commented Aug 28, 2012 at 19:05

5 Answers 5

4

All in one

var sEmailList = String.Join(",", 
      GetUserReportsToChain.Rows
      .Cast<DataRow>()
      .Select(m => m["usrEmailAddress"].ToString())
      .Where(x => !string.IsNullOrWhiteSpace(x)));
Sign up to request clarification or add additional context in comments.

2 Comments

Cast<DataRow> ==> Cast<DataRow>()
@JamesWilson : all but first: DataTable GetUserReportsToChain = objEmployee.GetUserReportsToChain(LoginID, ConfigurationManager.AppSettings.Get("Connection").ToString());
4

You want to use List<string> instead of fixed size array. Or simple LINQ query that returns non-empty strings.

List<string> aEmailList = new List<string>();
foreach (DataRow rRow in GetUserReportsToChain.Rows)
{
    if (!String.IsNullOrEmpty(rRow["usrEmailAddress"].ToString()))
    {
        aEmailList.Add(rRow["usrEmailAddress"].ToString());
    }
}
string sEmailList = String.Join(",", aEmailList);

3 Comments

Ok I am going to try and implement this solution.
It gives me this error on your very first line above. Error 1 'System.Collections.Generic.List<string>' is a 'type' but is used like a 'variable'
Needs the new keyword: List<string> aEmailList = new List<string>();
2

you can try with

String.Join(",", aEmailList.Where(s => !string.IsNullOrEmpty(s)));

Comments

2

You can change your if statement to:

if(!string.IsNullOrEmpty(rRow["usrEmailAddress"].ToString().Trim())

Comments

0

If you're using 3.0 or higher:

String.Join(",", aEmailList.Where(s => s.Trim() != ""));

2 Comments

This needs 4.0 to compile, not 3.0
Right you are. For 3.0, you would have to convert the result of "Where" with "ToArray".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.