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_ItemInser ted(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("Descri ption")
e.KeepInInsertMode = False
End If
Sub Email()
Dim credential As New System.Net.NetworkCredenti al("Domain \User", "Password")
Dim message As New System.Net.Mail.MailMessag e()
Dim client As New System.Net.Mail.SmtpClient ()
client.Credentials = credential
message.From = New System.Net.Mail.MailAddres s("FromUse r@Domain.c om.au")
message.To.Add(New System.Net.Mail.MailAddres s("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
Sub EmployeeFormView_ItemInser
' 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("Descri
e.KeepInInsertMode = False
End If
Sub Email()
Dim credential As New System.Net.NetworkCredenti
Dim message As New System.Net.Mail.MailMessag
Dim client As New System.Net.Mail.SmtpClient
client.Credentials = credential
message.From = New System.Net.Mail.MailAddres
message.To.Add(New System.Net.Mail.MailAddres
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
Try:
MessageLabel.Text = e.Values("Description")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have tried both.
VjSoft: Result was:
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
Line 54: message.To.Add(New System.Net.Mail.MailAddres s("ToUser@ Domain.com .au"))
Line 55: message.Subject = "You have been assigned a new task!"
Line 56: message.Body = CType(FormView1.FindContro l("Message Label"), 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
VjSoft: Result was:
Exception Details: System.NullReferenceExcept
Source Error:
Line 54: message.To.Add(New System.Net.Mail.MailAddres
Line 55: message.Subject = "You have been assigned a new task!"
Line 56: message.Body = CType(FormView1.FindContro
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?
What is MessageLabel? is it a control on FormView1?
ASKER
VjSoft,
When I try message.Body = CType(EmployeeFormView.ite ms.findcon trol("Mess ageLabel") , label).text
I get an error: 'EmployeeFormView' is not declared. it may be inaccessible due to its protection level
Thanks,
MMBS
When I try message.Body = CType(EmployeeFormView.ite
I get an error: 'EmployeeFormView' is not declared. it may be inaccessible due to its protection level
Thanks,
MMBS
ASKER
Yes it is a TextBox on formView1.
keep a breakpoint on
If e.AffectedRows = 1 Then
MessageLabel.Text = FormView1.DataItem("Descri ption")
e.KeepInInsertMode = False
End If
and check before calling Email() method, your value is going to be set in messagelabel or not?
If e.AffectedRows = 1 Then
MessageLabel.Text = FormView1.DataItem("Descri
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).
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).
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_Descri ptionTextB ox0")
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.MailMessag e()
Dim client As New System.Net.Mail.SmtpClient ()
message.From = New System.Net.Mail.MailAddres s("FromUse r@domain.c om.au")
message.To.Add(New System.Net.Mail.MailAddres s("ToUser@ domain.com .au"))
message.Subject = "Blah Blah Blah!"
message.Body = MessageLabel.Text
message.IsBodyHtml = True
client.Send(message)
End Sub
Regards,
MMBS
Sub TaskFormView_ItemInserted(
' 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_Descri
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.MailMessag
Dim client As New System.Net.Mail.SmtpClient
message.From = New System.Net.Mail.MailAddres
message.To.Add(New System.Net.Mail.MailAddres
message.Subject = "Blah Blah Blah!"
message.Body = MessageLabel.Text
message.IsBodyHtml = True
client.Send(message)
End Sub
Regards,
MMBS
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all I have resolved this with the following:
MessageLabel.Text = "Description : " + CType(FormView1.FindContro l("Descrip tionTextBo x0"), TextBox).Text
Greatly Appreciated!
Regards,
MMBS
MessageLabel.Text = "Description : " + CType(FormView1.FindContro
Greatly Appreciated!
Regards,
MMBS
message.Body = cast(EmployeeFormView.item
may be syntax is little bit wrong, but it should be similar to the above line.