troubleshooting Question

Build a string of emails from a gridview

Avatar of dodgerfan
dodgerfanFlag for United States of America asked on
C#ASP.NET
2 Comments1 Solution123 ViewsLast Modified:
I have a gridview in ASP.Net that display data from a database table. One of the fields is an email address. I am using stringbuilder to create a string of all of the emails to include elsewhere in an email message. I'm trying to get it formatted right. Right now it creates the string but there are extra spaces when I try to add a ; to the end of each email.

It comes out this way: test@email1 ; test2@email2 ; test3@email3 ;
I'd like I to appear this way: test@email1; test2@email2; test3@email3

My code looks like this right now:

public void getdata()
{
string connectionString = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;

using (SqlConnection sqCon = new SqlConnection(connectionString))
{
sqCon.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users", sqCon))
{
SqlDataReader reader = cmd.ExecuteReader();
gvNames.DataSource = reader;
gvNames.DataBind();
}}}

public void WriteString()
{
        StringBuilder str = new StringBuilder();
        foreach (GridViewRow row in gvNames.Rows)
        {
          str.AppendLine(row.Cells[0].Text).Append(";");
        }
        string strEmails = str.ToString();
        lblString.text = strEmails;
}
ASKER CERTIFIED SOLUTION
Kyle Abrahams
Director of Information Technology

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros