Solved

Email Confirmation From FormView control after inserting data

Posted on 2010-08-23
6
543 Views
Last Modified: 2012-05-10
I have a web app with a formview control to insert data into a sql database. I want to send a notification email of newly inserted data. can somebody please tell me how to do this?
0
Comment
Question by:MMBS
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Stephan
ID: 33508206
You can use the ItemInserted event on the FormView.
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 33509290
Write your mail sending code in a function and call that function on  FormView's ItemInserted Event
Check following link for ItemInserted Event code
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserted.aspx

Hope this will help you.
Regards,
Ram
0
 

Author Comment

by:MMBS
ID: 33517432
Thanks for the info. I used the itemInstered event but I still do not get any eamils. I do not get any errors messages either. Here is my code. What am I diong wrong?

----Web.config---









----Form Page----


<script runat="server">
    Sub EmployeeFormView_ItemInserted(ByVal sender As Object, ByVal e As FormViewInsertedEventArgs)
        ' Use the Exception property to determine whether an exception
        ' occurred during the insert operation.
        If e.Exception Is Nothing Then
            ' Use the AffectedRows property to determine whether the
            ' record was inserted. Sometimes an error might occur that
            ' does not raise an exception, but prevents the insert
            ' operation from completing.
            If e.AffectedRows = 1 Then
                MessageLabel.Text = FormView1.DataItem("Description")
                e.KeepInInsertMode = False
            End If
        Else
            ' Insert the code to handle the exception.
            MessageLabel.Text = e.Exception.Message
            ' Use the ExceptionHandled property to indicate that the
            ' exception has already been handled.
            e.ExceptionHandled = True
            e.KeepInInsertMode = False
        End If
    End Sub
    Sub Email()
        Dim credential As New System.Net.NetworkCredential("Domain\User", "Password")
        Dim message As New System.Net.Mail.MailMessage()
        Dim client As New System.Net.Mail.SmtpClient()
        client.Credentials = credential
       
        message.From = New System.Net.Mail.MailAddress("FromUser@Domain.com.au")
        message.To.Add(New System.Net.Mail.MailAddress("ToUser@Domain.com.au"))
        message.Subject = "blah blah vlah!"
        message.Body = MessageLabel.Text
        message.IsBodyHtml = True
        client.Send(message)
    End Sub
   
</script>
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 33518052
Hi MBBS,
I think there is nothing wrong in your code, but you have not called sub email anywhere. So just it as below:

Sub EmployeeFormView_ItemInserted(ByVal sender As Object, ByVal e As FormViewInsertedEventArgs)

       ' Use the Exception property to determine whether an exception
       ' occurred during the insert operation.
       If e.Exception Is Nothing Then

           ' Use the AffectedRows property to determine whether the
           ' record was inserted. Sometimes an error might occur that
            ' does not raise an exception, but prevents the insert
           ' operation from completing.
           If e.AffectedRows = 1 Then

               MessageLabel.Text = FormView1.DataItem("Description")
                e.KeepInInsertMode = False
               Email()      

           End If

       Else

           ' Insert the code to handle the exception.
           MessageLabel.Text = e.Exception.Message

           ' Use the ExceptionHandled property to indicate that the
            ' exception has already been handled.
           e.ExceptionHandled = True
           e.KeepInInsertMode = False

       End If

   End Sub

   Sub Email()
       Dim credential As New System.Net.NetworkCredential("Domain\User", "Password")
       Dim message As New System.Net.Mail.MailMessage()
       Dim client As New System.Net.Mail.SmtpClient()
       client.Credentials = credential
     
        message.From = New System.Net.Mail.MailAddress("FromUser@Domain.com.au")
       message.To.Add(New System.Net.Mail.MailAddress("ToUser@Domain.com.au"))
       message.Subject = "blah blah blah!"
       message.Body = MessageLabel.Text
       message.IsBodyHtml = True
       client.Send(message)
   End Sub


Hope this will help you.
Regards,
Ram
0
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 500 total points
ID: 33518062
MMBS,
Sorry for writing your name wrong.

Regards,
Ram
0
 

Author Comment

by:MMBS
ID: 33527260
Thanks Ram, you have me help a lot. I am getting the emails now but I am not getting the inserted value from textbox  in the formview control. Am I doing this correctly with the following code?
The TextBox ID is Description.
MessageLabel.Text = FormView1.DataItem("Description")
Rgards,
MMBS
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now