Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

retriveing Inserted data from a formview control

Posted on 2010-08-26
13
Medium Priority
?
336 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
ID: 33538815
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 75

Expert Comment

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

Open in new window

0
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 1000 total points
ID: 33538874
use CType instead of cast
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:MMBS
ID: 33538926
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 75

Expert Comment

by:käµfm³d 👽
ID: 33538937
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
ID: 33538949
yes, if you wrote the exact line that I provide you may throw error.

What is MessageLabel? is it a control on FormView1?
0
 

Author Comment

by:MMBS
ID: 33538953
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
ID: 33538957
Yes it is a TextBox on formView1.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33538961
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
ID: 33548289
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
ID: 33622671
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 1000 total points
ID: 33625759
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
ID: 33632424
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

886 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