Link to home
Start Free TrialLog in
Avatar of MMBS
MMBS

asked on

retriveing Inserted data from a formview control

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
Avatar of Mohit Vijay
Mohit Vijay
Flag of India image

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.

Avatar of kaufmed
Try:
MessageLabel.Text = e.Values("Description")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mohit Vijay
Mohit Vijay
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MMBS
MMBS

ASKER

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
Can you put a breakpoint at that line and verify that data is being inserted into the label's Text field?
yes, if you wrote the exact line that I provide you may throw error.

What is MessageLabel? is it a control on FormView1?
Avatar of MMBS

ASKER

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
Avatar of MMBS

ASKER

Yes it is a TextBox on formView1.
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?
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).
Avatar of MMBS

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MMBS

ASKER

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