Link to home
Create AccountLog in
Avatar of ICantSee
ICantSeeFlag for United States of America

asked on

Error handing in an asp.net web app

I am a novice developer and have a web app that has some error checking in  the code behind for its page.

If everything goes well the data is written to a database, an email is sent to me, and the user is supposed to be redirected.

The first two are happening, but the user is not being redirected. Its my suspicion that my error checking is never handled.

Any help with this will be greatly appreciated.

Imports System.Data.SqlClient
Imports System.Net.Mail
Partial Class ImpactSurvey
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Private _strsql As String
    Private Property strsql As String
        Get
            Return _strsql
        End Get
        Set(value As String)
            _strsql = value
        End Set
    End Property

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        con = New SqlConnection("Data Source=WEBSQL\WEBDATA;Initial Catalog=Service_Forms;Integrated Security=True")
        con.Open()
        strsql = "insert into AgencyServicesImpactSurvey (SurveyMedical, SurveyResources, SurveyMeetings, SurveyPaperwork, SurveyGroceries, SurveyPrograms, SurveyVision, SurveyIndependence, SurveySatisfied, SurveyComments) values ('" _
        & MedicalDropdownList.SelectedValue & "','" _
        & ResourcesDropdownlist.SelectedValue & "','" _
        & MeetingsDropdownlist.SelectedValue & "','" _
        & PaperworkDropdownlist.SelectedValue & "','" _
        & GroceriesDropdownlist.SelectedValue & "','" _
        & ProgramsDropdownlist.SelectedValue & "','" _
        & visionDropdownlist.SelectedValue & "','" _
        & IndependenceDropdownlist.SelectedValue & "','" _
        & SatisfiedDropdownlist.SelectedValue & "','" _
        & CommentsTextbox.Text & "')"
        cmd.CommandText = strsql
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        'MsgBox("Data Saved")
        con.Close()
        con.Dispose()


        'Capture fields for email body

        Dim fileName As String = Server.MapPath("~/App_Data/Survey.aspx")
        Dim mailBody As String = System.IO.File.ReadAllText(fileName)

        mailBody = mailBody.Replace("##MedicalLabel##", MedicalLabel.Text)
        mailBody = mailBody.Replace("##Medical##", MedicalDropdownList.SelectedValue)
        mailBody = mailBody.Replace("##ResourcesLabel##", ResourcesLabel.Text)
        mailBody = mailBody.Replace("##Resources##", ResourcesDropdownlist.SelectedValue)
        mailBody = mailBody.Replace("##MeetingsLabel##", MeetingsLabel.Text)
        mailBody = mailBody.Replace("##Meetings##", MeetingsDropdownlist.SelectedValue)
        mailBody = mailBody.Replace("##PaperworkLabel##", PaperworkLabel.Text)
        mailBody = mailBody.Replace("##Paperwork##", PaperworkDropdownlist.SelectedValue)
        mailBody = mailBody.Replace("##GroceriesLabel##", GroceriesLabel.Text)
        mailBody = mailBody.Replace("##Groceries##", GroceriesDropdownlist.SelectedValue)
        mailBody = mailBody.Replace("##ProgramsLabel##", ProgramsLabel.Text)
        mailBody = mailBody.Replace("##Programs##", ProgramsDropdownlist.SelectedValue)
        mailBody = mailBody.Replace("##VisionLabel##", VisionLabel.Text)
        mailBody = mailBody.Replace("##Vision##", visionDropdownlist.SelectedValue)
        mailBody = mailBody.Replace("##IndependenceLabel##", IndependenceLabel.Text)
        mailBody = mailBody.Replace("##Independence##", IndependenceDropdownlist.SelectedValue)
        mailBody = mailBody.Replace("##SatisifiedLabel##", SatisfiedLabel.Text)
        mailBody = mailBody.Replace("##Satisfied##", SatisfiedDropdownlist.SelectedValue)
        mailBody = mailBody.Replace("##CommentsLabel##", CommentsLabel.Text)
        mailBody = mailBody.Replace("##Comments##", CommentsTextbox.Text)



        'Create and send the email:

        Dim mySmtpClient As SmtpClient = New SmtpClient()
        Dim myMessage As New MailMessage()
        
        myMessage.Subject = "Agency Services Imapact Survey"
        myMessage.Body = mailBody
        myMessage.From = New MailAddress("webaapemail address")
        myMessage.IsBodyHtml = True
        myMessage.To.Add(New MailAddress("Myemailadress"))
        'mySmtpClient.Send(myMessage)

        Dim smtpClient As New SmtpClient()
        Dim userState As Object = myMessage

        AddHandler smtpClient.SendCompleted, AddressOf SmtpClient_OnCompleted

        Try
            'Send the email asynchronously
            smtpClient.SendAsync(myMessage, userState)
        Catch smtpEx As SmtpException
            'Error handling here
        Catch ex As Exception
            'Error handling here
        End Try
        'Attach event handler for async callback


    End Sub
    ''' <summary>
    ''' Event handler for processing completion information after asynchronous email sent.
    ''' </summary>
    Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

        'Get UserState as MailMessage instance from SendMail()
        Dim mailMessage As MailMessage = CType(e.UserState, MailMessage)

        If (e.Cancelled) Then
            FeedBackLabel.Text = "Sending of email message was cancelled. Address=" '+ mailMessage.To(0).Address
        End If

        If Not (e.Error Is Nothing) Then
            FeedBackLabel.Text = "Error occured. Please contact KBA's technical support at 724.347.5501 ext 252"
        Else
            Response.Redirect("default.aspx")
            mailMessage.Dispose()
        End If

    End Sub
End Class

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

>smtpClient.SendAsync(myMessage, userState)


What happens if you try Send instead?
Avatar of ICantSee

ASKER

Error      1      Overload resolution failed because no accessible 'Send' accepts this number of arguments.      \\data\kba\Departments\Information Technology\IT Team\websites\Sevice_Forms\ImpactSurvey.aspx.vb      88      13      Sevice_Forms
Try

smtpClient.Send(myMessage)
Thank you for your comments,

Ok. that ran, but did not redirect

       Dim smtpClient As New SmtpClient()
        Dim userState As Object = myMessage

        AddHandler smtpClient.SendCompleted, AddressOf SmtpClient_OnCompleted

    End Sub
    ''' <summary>
    ''' Event handler for processing completion information after asynchronous email sent.
    ''' </summary>
    Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

        'Get UserState as MailMessage instance from SendMail()
        Dim mailMessage As MailMessage = CType(e.UserState, MailMessage)

        If (e.Cancelled) Then
            FeedBackLabel.Text = "Sending of email message was cancelled. Address=" '+ mailMessage.To(0).Address
        End If

        If Not (e.Error Is Nothing) Then
            FeedBackLabel.Text = "Error occured. Please contact KBA's technical support at 724.347.5501 ext 252"
        Else
            Response.Redirect("default.aspx")
            mailMessage.Dispose()
        End If

    End Sub
End Class 

Open in new window

Move this line

Response.Redirect("default.aspx")

to right after the

smtpClient.Send(myMessage)

line.
ASKER CERTIFIED SOLUTION
Avatar of ICantSee
ICantSee
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Figured it out myself