Solved

Email Confirmation From FormView control after inserting data

Posted on 2010-08-23
6
546 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

770 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