tags:

views:

90

answers:

4

Hi, I need syntax help with the following code logic:

I have a code block that gets email address from the database. The email addresses need to be assigned to a string variable strEmailAddress with a comma seperation

My code is:

SqlConnection conn = new SqlConnection(strConn);
string sqlEmailAddress = "usp_Get_Email_Address";
SqlCommand cmdEmailAddr = new SqlCommand(sqlEmailAddress, conn);
cmdEmailAddr.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader sqlDREmailAddr = cmdEmailAddr.ExecuteReader();

How can I loop through the records and store the results in strEmailAddress seperated by comma?

Thanks

+5  A: 
while (sqlDREmailAddr.Read())
{
    //...process each row here
}

I would also wrap the reader in a using statement to make sure it is closed properly:

using (SqlDataReader sqlDREmailAddr = cmdEmailAddr.ExecuteReader())
{
}

Depending on what the columns in your dataset is named, reading values from each record will look something like this (update: now with all addresses merged):

var emailAddress = new StringBuilder();
var emailAddressOrdinal = sqlDREmailAddr.GetOrdinal("EmailAddress");
while (sqlDREmailAddr.Read())
{
    if (emailAddress.Length > 0)
        emailAddress.Append(',');
    emailAddress.Append(sqlDREmailAddr.GetString(emailAddressOrdinal));
}
Peter Lillevold
+1 for wrapping reader in using.
Oded
while (sqlDREmailAddr.read()){ strEmailAddress = ?????}
user279521
@user279521: added code for concatenating the addresses
Peter Lillevold
+1  A: 
while (sqlDREmailAddr.Read())
  {
    // handle row here
  }
gmcalab
The test for HasRows is rather superfluous since the first call to `Read()` will return false if there are no rows.
Peter Lillevold
I thought so too, I originally had my answer without it....but on MSDN's example they did both. So I put both. http://msdn.microsoft.com/en-us/library/haa3afyz%28VS.71%29.aspx
gmcalab
+3  A: 

Use the SqlDataReader.Read method:

while (sqlDREmailAddr.Read())
{
 ...
 // Assumes only one column is returned with the email address
 strEmailAddress = sqlDREmailAddr.GetString(0);
}
Oded
while (sqlDREmailAddr.read()) { strEmailAddress = ????? }
user279521
I don't know what your stored procedure returns. How does it show in the datarecord?
Oded
it returns a string; there can be more than one record returned;
user279521
Answer updated. Read up on SqlDataReader methods, you might learn something.
Oded
Thanks Oded.....
user279521
@Oded: OP wonders how to concatenate all addresses comma separated...
Peter Lillevold
+1  A: 

This is what you're looking for....

using (SqlConnection conn = new SqlConnection(strConn)){
   string sqlEmailAddress = "usp_Get_Email_Address";

   using (SqlCommand cmdEmailAddr = new SqlCommand(sqlEmailAddress, conn)){
       cmdEmailAddr.CommandType = CommandType.StoredProcedure;

       conn.Open(); // Typo Glitch!

       using (SqlDataReader sqlDREmailAddr = cmdEmailAddr.ExecuteReader()){

           while(sqlDREmailAddr.Read()){

              if (!sqlDREmailAddr.IsDBNull(sqlDREmailAddr.GetOrdinal("emailAddr"))){

                 // HANDLE THE DB NULL...

              }else{

                 strEmailAddress = sqlDREmailAddr.GetSqlString(sqlDREmailAddr.GetOrdinal("emailAddr"));

                 // Do something with strEmailAddr...

              }
           }
       }
   }

}

Notice:

  • A typo glitch on the conn variable...
  • A check is made to ensure that the Database value returned is not NULL
  • A call is made to GetOrdinal to return the column based on emailAddr string value that corresponds to the column from the query for SQL Select...which is an int type) as the parameter for GetSqlString..

Edit: Thanks to John Saunders for pointing out a blooper!

Edit#2: Thanks to Peter Lillevold for pointing out a mis-spelling...

Hope this helps, Best regards, Tom.

tommieb75
How about some using blocks?
John Saunders
Move your `GetOrdinal`s out of the loop. And `Read` is with capital R :)
Peter Lillevold
@Peter Lillevold: sigh...wish there was some intelligent spell checker to spot that one... yeah... good catch there.... :) nice one+1 for your comment!
tommieb75