Solved

Email Confirmation From FormView control after inserting data

Posted on 2010-08-23
6
547 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
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.

791 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