Results 1 to 20 of 20

Thread: [RESOLVED] Query Results to e-Mail

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Resolved [RESOLVED] Query Results to e-Mail

    I know this has probibly been asked before but I have had no luck finding an answer or example anywhere for what I want to do. Which Is.....

    I have a query that returns Lname, Fname, and EMail

    I want to send 1 email (that Is already set up and working) that contains the results of the query as part of the body that includes an additional message;
    "The persons listed below.........." Need to know how to get them "listed below".

    then I want to send a seprate e-mail to each email address the query returns, sepratly not in a group but the same message.

    any help, examples or links would be appreciated.
    Thanks

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    Take the result (is if in a datareader or DataTable) and go though each row.

    Append the fields of each row into a string var. Start the string var with:

    The persons listed below.........
    then Set that result to the body text of the eamil
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    Gary,

    I know this isn't right, but am I going in the right direction?

    Code:
    Dim strBuilder As New System.Text.StringBuilder
                    strBuilder.Append("The following Persons....."(table.Rows))

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    You are starting.

    Is the result set a DataTable or a DataReader?

    You cycle though each row. Adding each field to the string.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    Datatable "table" How?

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    What is the code that brings back the result set?

    da.Fill?

    When you have a datatable you have a method called RowCount().

    You do a for loop and read each line of the resulting DataTable
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    using an OledbDataAdapter to fill dataTable

    Trying something like this will it work?
    Code:
     Dim strBuilder As New System.Text.StringBuilder
                    For Each row As DataRow In table.Rows
                        strBuilder = New System.Text.StringBuilder
    
                        For Each col As DataColumn In table.Columns
                            ' Output the value of each column's data.
                            strBuilder.Append("The following Persons....."(row(col).ToString() & ", "))
                        Next
                      
                    Next
    Then just but the string value in the eMail body?

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    Move this:

    strBuilder = New System.Text.StringBuilder

    outside the loop
    at the end of the the inside loop add

    strBuilder.System.Enviorment.NewLine
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    strBuilder.System.Enviorment.NewLine

    is not a member of System.Text.StringBuilder

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    Sorry should be strBuilder.Append (System.Evniorment.NewLine)

    That will place each row on its own line.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    Ok Great now how do I get it to the body of my email?

    I know I can't just say body = strBuilder

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    Have you done email yet in an application?

    You will need to set this all up to be able to use it.

    You will need to say what the SMTP server name is, If there are passwords and usernames involved. What the email program is.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    Have it all set up and it works using Outlook

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    just nee to know how to get what I just did in the body of the message

  15. #15
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    Then you should just be able to assign the resulting string to the body of the mail message.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    Just tried to run it got an "InvalidCastException" Here

    Code:
     strBuilder.Append("The following Persons....."(row(col).ToString() & ", "))
    Conversion from String "GORDEN" to type integer is not valid

  17. #17
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    vb.net Code:
    1. strBuilder.Append("The following Persons....."
    2. strBuilder.Append(System.Enviorment.NewLine)
    3. Dim iCols as Integer = table.Coumns.Count() -1
    4.                 For iRow as Integer = 0 to table.Rows.RowCount()-1
    5.                     For Each iCol as Integer = 0 to iCols
    6.                         strBuilder.Append(table.Rows(iRow).Item(iCol).ToString() & ", ")
    7.                     Next
    8.                   strBuilder.Append(System.Enviorment.NewLine)
    9.                 Next
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    I doesn't like the = in For Each iCol as Integer = 0 to iCols
    Syntax error

  19. #19
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Query Results to e-Mail

    For iCols as Integer = 0
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    94

    Re: Query Results to e-Mail

    That seems to be working
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width