|Contact Form w/ Confirmation Page (missing something small)|
ASP.NET / VB Contact Form
I am new to ASP.NET, and reading through online materials such as this forum has really helped me gather a broad scope about what this language is and does. It's a great forum here, and I have referenced it many times.
I am working on a basic contact form in ASPX with VB with four easy textboxes. On submit, I want the form to be sent to an e-mail address and have the sender be sent to a confirmation page. For the life of me, I cannot find the mistake that is causing my confirmation redirect to be unread. On submit, it always goes to my error page.
I have already completed a form like this and have even tried working from this file; however, I cannot manage to find the solution. I am on a deadline of 24 hours to complete this, and I have already been pulling my hair for a few days. I know it's something so simple, yet my eyes are missing it from too much view probably. May someone lend me your eyes and review my code below? I would truly appreciate your time and help.
I have the full page code, but the problem seems to only be in the scripting tags. For quick view, only this is pasted below:
<%@ Page language="VB" masterpagefile="orientation.master" title="New Student Orientation Request" %>
<%@ MasterType VirtualPath="orientation.master" %>
<%@ Import Namespace="System.Net.Mail" %>
<asp:Content id="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1">
<script runat="server" type="text/vb">
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
Dim currentDate As DateTime = DateTime.Now
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsValid Then
Dim strFirstName, strLastName, strStudentNumber As String
Dim strEmailAddress As String
Dim mpContentPlaceHolder As ContentPlaceHolder
Dim strFrom, strTo, strSubject, strBody As String
Dim msgBody As String
mpContentPlaceHolder = CType(Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
strFirstName = Left(CType(mpContentPlaceHolder.FindControl("FirstName"), TextBox).Text, 25)
strLastName = Left(CType(mpContentPlaceHolder.FindControl("LastName"), TextBox).Text, 25)
strStudentNumber = Left(CType(mpContentPlaceHolder.FindControl("StudentNumber"), TextBox).Text, 7)
strEmailAddress = Left(CType(mpContentPlaceHolder.FindControl("EmailAddress"), TextBox).Text, 50)
msgBody = "First: " & strFirstName & vbCrLf & "Last: " & strLastName & vbCrLf & "Number: " & strStudentNumber & vbCrLf & "Email: " & strEmailAddress & vbCrLf &
Dim fromAddress As New MailAddress(strEmailAddress)
Dim toAddress As New MailAddress("email@example.com")
Dim msg As New MailMessage(fromAddress, toAddress)
msg.Subject = "Orientation Cancellation Request"
msg.Body = msgBody
Dim mailSender As New SmtpClient()
mailSender.Host = "www.host.com"
Catch ex As Exception
Thank you again for any help you may have to offer. I would also like to help self-teach myself ASP.NET. If you have recommendations of books, that would be wonderful as well.
All the best,
My best guess that it fails before the email gets sent or the redirect is even reached. And that it not finding one of the controls for some reason.
If you comment out all the pieces relating to sending the mail, does it still error? If so, you might try uncommenting line by line to try and find the trouble spot.
I wouldn't use a redirect. That's more for classic ASP. Try using a view state.
Thank you all for your responses. I ended up using a different approach and got it to work somehow. I still want to jump back and play with this code a bit so I can resolve it. I have not heard of view state, but I will look into this. Yes, I have tried commenting out many areas to see if that was an issue as well, but I didn't get far with that unfortunately.
Thank you all for the help. I will look into it some more and keep you posted with what I find, if anything.