Solved

retriveing Inserted data from a formview control

Posted on 2010-08-26
13
331 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
[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
  • 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 250 total points
ID: 33538874
use CType instead of cast
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 

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 250 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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