Results to both Database AND email

D

dancer

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."
 
G

Guest

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
 
G

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."

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
 
G

Guest

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
 
D

dancer

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

Thank you.
 
G

Guest

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top