C# asp.net calling event from another event handler

I am very new to c# and asp.net, so thank you in advance.

I'm writing a web app that when the user fills out a form, it inserts a record into the database, and also sends an html formatted email to various people with the form results.

The problem Im having is that the "Submit" button naturally is only firing the Button1_Click event, and not the remainder of the code that contains the email sending process.

How do I fire the "Email Event" from the Button Click event?  Ive included code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mail;
using System.Diagnostics;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Net;
using System.Text;
using System.IO;
using System.Reflection;

namespace InventoryTransferDashboard
    public partial class New_Transfer_Request : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)


        public void Button1_Click(object sender, EventArgs e)
            SqlConnection con = new SqlConnection(@"Data Source=******");
                //Inserts New Record
                SqlCommand cmd = new SqlCommand("INSERT INTO Transfers(StockNo, SerNo, Model, ModelDesc, ShipBranch, ReqBranch, Customer, Instructions, SubmitDateTime, SubmitBy, Status, Visible) VALUES (@StockNo, @SerNo, @Model, @ModelDesc, @ShipBranch, @ReqBranch, @Customer, @Instructions, @SubmitDateTime, @SubmitBy, @Status, @Visible)", con);
                cmd.Parameters.AddWithValue("@StockNo", tb_su.Text);
                cmd.Parameters.AddWithValue("@SerNo", tb_ser.Text);
                cmd.Parameters.AddWithValue("@Model", tb_model.Text);
                cmd.Parameters.AddWithValue("@ModelDesc", tb_desc.Text);
                cmd.Parameters.AddWithValue("@ShipBranch", cb_shipbranch.Text);
                cmd.Parameters.AddWithValue("@ReqBranch", cb_reqbranch.Text);
                cmd.Parameters.AddWithValue("@Customer", tb_customer.Text);
                cmd.Parameters.AddWithValue("@Instructions", tb_instructions.Text);
                cmd.Parameters.AddWithValue("@SubmitDateTime", DateTime.Now);
                cmd.Parameters.AddWithValue("@SubmitBy", tb_submitby.Text);
                cmd.Parameters.AddWithValue("@Status", "Requested");
                cmd.Parameters.AddWithValue("@Visible", "1");
                SqlCommand cmd2 = new SqlCommand("SELECT * FROM Branch br WHERE br.Branch = '" + cb_reqbranch.Text + "'", con);
                SqlCommand cmd3 = new SqlCommand("SELECT * FROM Branch br WHERE br.Branch = '" + cb_shipbranch.Text + "'", con);
                SqlDataReader rRouteEmail = cmd2.ExecuteReader();
                string rEmail = String.Empty;
                string sbEmail = String.Empty;
                while (rRouteEmail.Read())
                    rEmail = (string)rRouteEmail["RouteEmail"];
                    sbEmail = (string)rRouteEmail["BranchEmail"];
                    System.Diagnostics.Debug.WriteLine("Route Originator: " + rEmail);
                    System.Diagnostics.Debug.WriteLine("Shipping Branch: " + sbEmail);
                SqlDataReader sRouteEmail = cmd3.ExecuteReader();
                string sEmail = String.Empty;
                while (sRouteEmail.Read())
                    sEmail = Convert.ToString(sRouteEmail["RouteEmail"]);
                    System.Diagnostics.Debug.WriteLine("Some Other Email: " + sEmail);

//Email Code below will not run...nor do I know how to call it.

        public string SendTransferEmail(string rEmail, string sbEmail, string sEmail, string tb_su, string tb_ser, string tb_model, string tb_desc, string cb_reqbranch, string cb_shipbranch, string tb_customer, string tb_submitby, string tb_instructions)
                SmtpMail.SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"]; //Read all the configuration values from web.config.  
                MailMessage oMessage = new MailMessage();
                oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 0;
                oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = System.Configuration.ConfigurationManager.AppSettings["SmtpUser"];
                oMessage.BodyFormat = MailFormat.Html;
                oMessage.From = System.Configuration.ConfigurationManager.AppSettings["SmtpUser"].ToString();
                oMessage.Subject = "**New Inventory Transfer Request**";
                oMessage.To = rEmail + ";" + sEmail + ";" + sbEmail;
                oMessage.Headers.Add("content-type", "text/html;");
                string str = "";
                string FilePath = System.Configuration.ConfigurationManager.AppSettings["TemplatePath"].ToString();
                if (File.Exists(FilePath + "email.html"))
                    FileStream f1 = new FileStream(FilePath + "email.html", FileMode.Open);
                    StreamReader sr = new StreamReader(f1);
                    str = sr.ReadToEnd();
                    str = str.Replace("%tb_su%", tb_su);
                    str = str.Replace("%tb_ser%", tb_ser);
                    str = str.Replace("%tb_model%", tb_model);
                    str = str.Replace("%tb_desc%", tb_desc);
                    str = str.Replace("%cb_reqbranch%", cb_reqbranch);
                    str = str.Replace("%cb_shipbranch%", cb_shipbranch);
                    str = str.Replace("%tb_customer%", tb_customer);
                    str = str.Replace("%tb_submitby%", tb_submitby);
                    str = str.Replace("%tb_instructions%", tb_instructions);
                oMessage.Body = str;
                return "Mail Sent Successfully!";
            catch (Exception ex)
                return "Error: Mail cannot be sent!";


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
SendTransferEmail is a function, not an event, so it can be called like any other function from wherever you would like it called in your Button1_Click event, just pass the appropriate values to it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
Technically speaking, none of the displayed code is an event...you have functions and event handlers. And event handlers are they themselves functions which are callable--as long as you provide the appropriate parameters. However, I do not suggest invoking one event handler from another event handler. As someone coming behind you, if I set a breakpoint in that second handler, expecting that its associated event was raised, then when the first event that was raised causes this second handler to be invoked, I'm going to be confused as to how this second event got raised. It would be better to refactor out the common logic that you want to invoke in both handlers into its own separate method. Then call that new method from both event handlers. Now there are no surprise results in term of how the event handlers were invoked.
hhnetworksAuthor Commented:
Thanks Shaun!

That cleared that up and I can tell Im past that hangup. Im now studying how to pass all 12 string values appropriately to the string function, but that would be a different question.
hhnetworksAuthor Commented:
Thanks Kaufmed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.