Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

retriveing Inserted data from a formview control

Posted on 2010-08-26
13
Medium Priority
?
333 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 1000 total points
ID: 33538874
use CType instead of cast
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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