I have created a 'Contact Us' form for my ASPX website. At the moment, there are two sections: one for general feedback, and a second where users can enter event details & send them to the admin email.

When the user fills out one of these sections, & clicks the relevant submit button, provided all fields are validated, the data they entered will be sent to admin email.

If I remove one of these sections/fieldsets, the remaining fieldset works perfectly, and the email is sent.

However, when both fieldsets are included (see below), I am getting this error you can see below.

Here is my ASPX:

<div class="row">
            <div class="col-md-4">
                <h3>General Feedback</h3>
                <fieldset>
                    <div class="control-group form-group">
                        <div class="controls">
                            <table>
                                <tr>
                                    <td>
                                        <b>Name:</b>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtName" Width="200px" CssClass="form-control" runat="server"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
                                            ErrorMessage="Name is required" ControlToValidate="txtName"
                                            Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Email:</b>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtEmail" Width="200px" CssClass="form-control" runat="server"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
                                            ErrorMessage="Email is required" ControlToValidate="txtEmail"
                                            Display="Dynamic" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                                            ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Please enter a valid email"
                                            ControlToValidate="txtEmail" ForeColor="Red">*</asp:RegularExpressionValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Subject:</b>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtSubject" Width="200px" CssClass="form-control" runat="server"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                                            ErrorMessage="Subject is required" ControlToValidate="txtSubject"
                                            Display="Dynamic" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="vertical-align: top">
                                        <b>Comments:</b>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtComments" Width="200px" CssClass="form-control" runat="server" Rows="5" TextMode="MultiLine"></asp:TextBox>
                                    </td>
                                    <td style="vertical-align: top">
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                                            ErrorMessage="Comments are required" ControlToValidate="txtComments"
                                            Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <div id="success">

                        <asp:Button ID="btnSendFeedback" CssClass="btn btn-primary" runat="server" Text="Submit" OnClick="btnSendFeedback_Click" />
                        <asp:ValidationSummary HeaderText="Please fix the following errors" ForeColor="Red" ID="ValidationSummary2" runat="server" />
                        <asp:Label ID="Label1" runat="server" Font-Bold="true"></asp:Label>
                    </div>

                </fieldset>

            </div>
            <!--SUGGEST EVENT COLUMN-->
            <div class="col-md-4">
                <h3>Suggest an Event</h3>
                <fieldset>
                    <div class="control-group form-group">
                        <div class="controls">
                            <table>
                                <tr>
                                    <td>
                                        <b>Name:</b>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtNameEventSuggestor" Width="200px" CssClass="form-control" runat="server"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                                            ErrorMessage="Name is required" ControlToValidate="txtNameEventSuggestor"
                                            Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Email:</b>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtEmailEventSuggestor" Width="200px" CssClass="form-control" runat="server"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
                                            ErrorMessage="Email is required" ControlToValidate="txtEmailEventSuggestor"
                                            Display="Dynamic" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                                            ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Please enter a valid email"
                                            ControlToValidate="txtEmailEventSuggestor" ForeColor="Red">*</asp:RegularExpressionValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Event Title:</b>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtEventTitle" Width="200px" CssClass="form-control" runat="server"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
                                            ErrorMessage="Subject is required" ControlToValidate="txtEventTitle"
                                            Display="Dynamic" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="vertical-align: top">
                                        <b>Event Description:</b>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtEventDescription" Width="200px" CssClass="form-control" runat="server" Rows="5" TextMode="MultiLine"></asp:TextBox>
                                    </td>
                                    <td style="vertical-align: top">
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
                                            ErrorMessage="Comments are required" ControlToValidate="txtEventDescription"
                                            Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div id="suggestEventSubmit">
                        <asp:Button ID="btnSubmitEventSuggestion" CssClass="btn btn-primary" runat="server" Text="Submit" OnClick="btnSubmitEventSuggestion_Click" />
                        <asp:ValidationSummary HeaderText="Please fix the following errors" ForeColor="Red" ID="ValidationSummary1" runat="server" />
                        <asp:Label ID="Label2" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                </fieldset>
            </div>
        </div>

This code displays the following: snip1

And here is my Code-Behind:

protected void btnSendFeedback_Click(object sender, EventArgs e)
{
    try
    {
        if (Page.IsValid)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("EMAIL@gmail.com"); //txtEmail.Text.ToString()
            mailMessage.To.Add("EMAIL@gmail.com"); //can have multiple here
            mailMessage.Subject = txtSubject.Text;
            mailMessage.Body = "<b>Sender Name:</b>" + txtName.Text + "<br/>"
               + "<b>Sender Email:</b>" + txtEmail.Text + "<br/>"
               + "<b>Comments:</b>" + txtComments.Text;

            //for the HTML tags mentioned in the body above
            mailMessage.IsBodyHtml = true;


            //IN THE TUTORIAL, HE IS USING GMAIL'S SMTP SERVER:
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.Credentials =
                new System.Net.NetworkCredential("EMAIL@gmail.com", "PASSWORD");
            smtpClient.Send(mailMessage);

            Label1.Text = "Thank you for contacting us";
            Label1.ForeColor = System.Drawing.Color.Blue;
            txtName.Enabled = false;
            txtEmail.Enabled = false;
            txtComments.Enabled = false;
            txtSubject.Enabled = false;
            btnSendFeedback.Enabled = false;
        }


    }
    catch (Exception ex)
    {
        // Log - Event Viewer or table
        Label1.ForeColor = System.Drawing.Color.Red;
        Label1.Text = "There is an unknown problem. Please try later";

    }
}



protected void btnSubmitEventSuggestion_Click(object sender, EventArgs e)
{
    try
    {
        if (Page.IsValid)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("EMAIL@gmail.com"); //txtEmail.Text.ToString()
            mailMessage.To.Add("EMAIL@gmail.com"); //can have multiple here
            mailMessage.Subject = "Suggested Event Title: " + txtEventTitle.Text;
            mailMessage.Body = "<b>Sender Name:</b>" + txtNameEventSuggestor.Text + "<br/>" + "<br/>"
               + "<b>Sender Email:</b>" + txtEmailEventSuggestor.Text + "<br/>" + "<br/>"
               + "<b>Comments:</b>" + txtEventDescription.Text;

            //for the HTML tags mentioned in the body above
            mailMessage.IsBodyHtml = true;


            //IN THE TUTORIAL, HE IS USING GMAIL'S SMTP SERVER:
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.Credentials =
                new System.Net.NetworkCredential("EMAIL@gmail.com", "PASSWORD");
            smtpClient.Send(mailMessage);

            Label2.Text = "Thank you for contacting us";
            Label2.ForeColor = System.Drawing.Color.Blue;
            txtNameEventSuggestor.Enabled = false;
            txtEmailEventSuggestor.Enabled = false;
            txtEventDescription.Enabled = false;
            txtEventTitle.Enabled = false;
            btnSubmitEventSuggestion.Enabled = false;
        }


    }
    catch (Exception ex)
    {
        // Log - Event Viewer or table
        Label2.ForeColor = System.Drawing.Color.Red;
        Label2.Text = "There is an unknown problem. Please try later";

    }
}

When I remove one of the fieldsets, & their corresponding Code-behind, the remaining code executes perfectly, and the email is sent.

However, when both fieldsets are there, and I fill out one section correctly, this is what happens:

snp2

Can anyone inform me as to what is happening, and how I can prevent it?

Related posts

Recent Viewed