Solved

retriveing Inserted data from a formview control

Posted on 2010-08-26
13
327 Views
Last Modified: 2012-08-13
I have a FormView control that sends an email when data is inserted. I want get the inserted data and include it in the body of the email. The textbox ID is DescriptionTextBox0. I have tried the following:

   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



   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

The email sends ok but does't capture the inserted data from the TextBox. Please Help.

Thanks,
MMBS
0
Comment
Question by:MMBS
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
try this

message.Body = cast(EmployeeFormView.items.findcontrol("MessageLabel"), label).text

may be syntax is little bit wrong, but it should be similar to the above line.

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Try:
MessageLabel.Text = e.Values("Description")

Open in new window

0
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 250 total points
Comment Utility
use CType instead of cast
0
 

Author Comment

by:MMBS
Comment Utility
I have tried both.
VjSoft: Result was:
 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 54:         message.To.Add(New System.Net.Mail.MailAddress("ToUser@Domain.com.au"))
Line 55:         message.Subject = "You have been assigned a new task!"
Line 56:         message.Body = CType(FormView1.FindControl("MessageLabel"), Label).Text
Line 57:
Line 58:         message.IsBodyHtml = True
 
kaufmed: Result was: I got the email but empty body.
Thanks to you both. Any other Ideas?
MMBS
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Can you put a breakpoint at that line and verify that data is being inserted into the label's Text field?
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
yes, if you wrote the exact line that I provide you may throw error.

What is MessageLabel? is it a control on FormView1?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:MMBS
Comment Utility
VjSoft,

When I try message.Body = CType(EmployeeFormView.items.findcontrol("MessageLabel"), label).text
I get an error:  'EmployeeFormView' is not declared. it may be inaccessible due to its protection level

Thanks,
MMBS
0
 

Author Comment

by:MMBS
Comment Utility
Yes it is a TextBox on formView1.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
keep a breakpoint on

If e.AffectedRows = 1 Then

               MessageLabel.Text = FormView1.DataItem("Description")

               e.KeepInInsertMode = False

           End If

and check before calling Email() method, your value is going to be set in messagelabel or not?
0
 
LVL 1

Expert Comment

by:MartinGreen
Comment Utility
From what I can see it looks like MessageLabel has somehow gone out of scope.

If as the poster above suggests, the data is going into MessageLabel.Text ok, can you advise if the form is being closed before the program gets to the sendmail part? It could be the control has been disposed before you read the data back out.

If that is the case, you may be better of storing the data in a member variable to the application or passing it back in a more elegant way (without seeing more of your code I can't guess at your structure).
0
 

Author Comment

by:MMBS
Comment Utility
Thank all for your help thus far. I am getting the email but all I get is the word Description: in the body of the mail. no insterted data from the textbox. What am I doing wrong?

Sub TaskFormView_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 = "Description : " + e.Values("FormView1_DescriptionTextBox0")

e.KeepInInsertMode = False
Email()
Else

MessageLabel.Text = "An error occurred during the insert operation."

' Use the KeepInInsertMode property to remain in insert mode
' when an error occurs during the insert operation.
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 message As New System.Net.Mail.MailMessage()
Dim client As New System.Net.Mail.SmtpClient()

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

Regards,
MMBS
0
 
LVL 1

Assisted Solution

by:MartinGreen
MartinGreen earned 250 total points
Comment Utility
First of all, may I suggest that you use the ampersand operator to append the text to MessageLabel.Text. I.e rather than:
MessageLabel.Text = "Description : " + e.Values("FormView1_DescriptionTextBox0")
Try:
MessageLabel.Text = "Description : " & e.Values("FormView1_DescriptionTextBox0")
Sometimes the '+' operator can do funny things with strings, however, I grant you this may not be the cause of your issue, however it's an easy modification so give it a go ;)

Secondly, you haven't really given us the results of the tests that were suggested above, so it is still difficult for us to help you. Can you come back to us with the results of those tests and I for one will try and give you some more help.

Good luck

Martin

0
 

Author Comment

by:MMBS
Comment Utility
Thank you all I have resolved this with the following:
 MessageLabel.Text = "Description : " + CType(FormView1.FindControl("DescriptionTextBox0"), TextBox).Text
Greatly Appreciated!
Regards,
MMBS
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

9 Experts available now in Live!

Get 1:1 Help Now