Results to both Database AND email

Discussion in 'ASP .Net' started by dancer, Aug 14, 2007.

  1. dancer

    dancer Guest

    How do I send results of a form page to both email AND a database. I have a
    page which works with this sub:

    Sub btnSendMail_OnClick(Source As Object, E As EventArgs)

    And I have a page which works with this sub:

    Sub btnSendDatabase_OnClick(Source As Object, E As EventArgs)

    How do I combine the two? I tried putting the second sub within the first,
    but got this error message:
    "Statement cannot appear within a method body. End of method assumed."
     
    dancer, Aug 14, 2007
    #1
    1. Advertising

  2. As a general rule, its not a good idea to put business logic inside event
    handlers such as "button_click". To make it really OOP you want to have the
    business logic in methods of a class library. In the Button_Click, you would
    make one call to your library's database "thing" and another to its (or
    another library's) "email" thing. Hope that makes sense.
    -- Peter
    Recursion: see Recursion
    site: http://www.eggheadcafe.com
    unBlog: http://petesbloggerama.blogspot.com
    BlogMetaFinder: http://www.blogmetafinder.com



    "dancer" wrote:

    > How do I send results of a form page to both email AND a database. I have a
    > page which works with this sub:
    >
    > Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
    >
    > And I have a page which works with this sub:
    >
    > Sub btnSendDatabase_OnClick(Source As Object, E As EventArgs)
    >
    > How do I combine the two? I tried putting the second sub within the first,
    > but got this error message:
    > "Statement cannot appear within a method body. End of method assumed."
    >
    >
    >
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Aug 14, 2007
    #2
    1. Advertising

  3. On Aug 14, 10:14 pm, "dancer" <> wrote:
    > How do I send results of a form page to both email AND a database. I have a
    > page which works with this sub:
    >
    > Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
    >
    > And I have a page which works with this sub:
    >
    > Sub btnSendDatabase_OnClick(Source As Object, E As EventArgs)
    >
    > How do I combine the two? I tried putting the second sub within the first,
    > but got this error message:
    > "Statement cannot appear within a method body. End of method assumed."


    1) Move the code out of the onclick-methods:

    Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
    SendDatabase()
    SendEmail()
    End Sub

    Sub btnSendDatabase_OnClick(Source As Object, E As EventArgs)
    SendDatabase()
    SendEmail()
    End Sub

    Sub SendDatabase()
    ' Code to save data
    .....
    End Sub

    Sub SendEmail()
    ' Code to send email
    .....
    End Sub

    2) Change event handler of your buttons to the same method.

    Create new method

    Protected Sub SendDatabaseAndEmail(Source As Object, E As EventArgs)
    Handles btnSendMail.Click, btnSendDatabase
    ' Code to save database and email
    ....
    End Sub

    And delete btnSendDatabase_OnClick and btnSendMail_OnClick methods
     
    Alexey Smirnov, Aug 14, 2007
    #3
  4. On Aug 14, 10:32 pm, Alexey Smirnov <> wrote:
    > Protected Sub SendDatabaseAndEmail(Source As Object, E As EventArgs)
    > Handles btnSendMail.Click, btnSendDatabase


    must be

    Protected Sub SendDatabaseAndEmail(Source As Object, E As EventArgs)
    Handles btnSendMail.Click, btnSendDatabase.Click
     
    Alexey Smirnov, Aug 14, 2007
    #4
  5. dancer

    dancer Guest

    Forgive me for being so dumb, but are you saying, do 1) *OR* 2)?
    Or are you saying do 1) *and* 2)?

    Thank you.

    "Alexey Smirnov" <> wrote in message
    news:...
    > On Aug 14, 10:14 pm, "dancer" <> wrote:
    >> How do I send results of a form page to both email AND a database. I
    >> have a
    >> page which works with this sub:
    >>
    >> Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
    >>
    >> And I have a page which works with this sub:
    >>
    >> Sub btnSendDatabase_OnClick(Source As Object, E As EventArgs)
    >>
    >> How do I combine the two? I tried putting the second sub within the
    >> first,
    >> but got this error message:
    >> "Statement cannot appear within a method body. End of method assumed."

    >
    > 1) Move the code out of the onclick-methods:
    >
    > Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
    > SendDatabase()
    > SendEmail()
    > End Sub
    >
    > Sub btnSendDatabase_OnClick(Source As Object, E As EventArgs)
    > SendDatabase()
    > SendEmail()
    > End Sub
    >
    > Sub SendDatabase()
    > ' Code to save data
    > ....
    > End Sub
    >
    > Sub SendEmail()
    > ' Code to send email
    > ....
    > End Sub
    >
    > 2) Change event handler of your buttons to the same method.
    >
    > Create new method
    >
    > Protected Sub SendDatabaseAndEmail(Source As Object, E As EventArgs)
    > Handles btnSendMail.Click, btnSendDatabase
    > ' Code to save database and email
    > ...
    > End Sub
    >
    > And delete btnSendDatabase_OnClick and btnSendMail_OnClick methods
    >
     
    dancer, Aug 14, 2007
    #5
  6. On Aug 14, 11:26 pm, "dancer" <> wrote:
    > Forgive me for being so dumb, but are you saying, do 1) *OR* 2)?


    Yes, it's either 1) OR 2)

    In 1) you will have two separated methods (as it is now in your code)
    for two buttons/events. Both methods will call two other new methods
    SendDatabase() and SendEmail(), where you should place your
    correspondent code. You also can combine these two methods in one, say
    SendDatabaseAndEmail()

    for example

    Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
    SendDatabaseAndEmail()
    End Sub

    Sub btnSendDatabase_OnClick(Source As Object, E As EventArgs)
    SendDatabaseAndEmail()
    End Sub

    Sub SendDatabaseAndEmail()
    ' Code to save data and send an email
    .....
    End Sub

    In 2) I suggested to use point both events to the same method.

    As Peter said, it's not always a good idea to put business logic
    inside "button_click". As you can see from my examples it's more easy
    sometimes to have it in a separated methods and if, for example, you
    would need to send an email from other web forms as well, it's really
    make sense to move SendEmail() to a class library which can be
    accessed from other pages.
     
    Alexey Smirnov, Aug 14, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Dong
    Replies:
    0
    Views:
    473
    Tony Dong
    Dec 20, 2005
  2. Monique Y. Mudama
    Replies:
    1
    Views:
    478
    Monique Y. Mudama
    Jun 28, 2005
  3. lovecreatesbeauty
    Replies:
    8
    Views:
    1,738
    Old Wolf
    Sep 12, 2005
  4. ABCL
    Replies:
    0
    Views:
    593
  5. Irishmaninusa

    Sending email both as html and plain text

    Irishmaninusa, Nov 13, 2003, in forum: ASP General
    Replies:
    2
    Views:
    136
    Irishmaninusa
    Nov 14, 2003
Loading...

Share This Page