Solved

Email Confirmation From FormView control after inserting data

Posted on 2010-08-23
6
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
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…

733 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