e.newvalues, e.oldvalues, AHH!! Driving me crazy.

razzi8990

New member
Joined
Apr 5, 2007
Messages
2
Programming Experience
Beginner
I'm trying to do something very simple, at least I thought it was until I spent the last couple days trying to figure out why it is not working. I would like to email the value that is being changed on RowUpdate of my Gridview. I'm using this code and get the error below:

Protected Sub GridView2_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView2.RowUpdating

Dim message As New MailMessage("username@company.com", "john_doe@compay.com", "Subject", e.Keys.Values)

Dim emailClient As New SmtpClient("smtpserver")
emailClient.Send(message)

End Sub
End
Class



Server Error in '/WebSite29' Application.

Unable to cast object of type 'OrderedDictionaryKeyValueCollection' to type 'System.String'.
 
Dont post ASP.NET questions in the VB.NET section.

e.Keys.Values is a Collection of things. It cannot be turned into a string using any built in lethod. THis is merely what the error message is telling you.

Either enumerate (skip over) the colelction and build the string yourself, or find the specific string that is being changed and send just that..

The code will look something like this:

VB.NET:
DIm sb as New StringBuilder
For Each o as WHATEVER in e.Keys.Values
  sb.Append(o)
Next o
...
sb.ToString() ....
BUt not exactly because Ive never heard of what youre using..

If youre not sure of the difference between a collection and a string, spend some time hammering google :D
 
Thanks, I was able to figure out how to get it working using the code below. However, I haven't been able to figure out how to get it to email just the deltas. In the example below it will email the entire new record and the entire old record. I really just wanted to see what changed and email that. If you have any suggestion I'd appreciate it.


VB.NET:
Protected Sub GridView2_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView2.RowUpdated
Dim sb As New StringBuilder()
For Each de As DictionaryEntry In e.NewValues
sb.AppendFormat("{0}: {1}" & Chr(10) & "", de.Key, de.Value)
Next
 
Dim old As New StringBuilder()
For Each df As DictionaryEntry In e.OldValues
old.AppendFormat("{0} was {1}" & Chr(10) & "", df.Key, df.Value)
Next
Dim message As New MailMessage("user@company.com", "user@company.com", _
"DB has been modified by " + User.Identity().Name, sb.ToString + vbCrLf + old.ToString)
Dim emailClient As New SmtpClient("smtp")
emailClient.Send(message)
End Sub
 
Last edited by a moderator:
You could always compare old and new value. This is perhaps around what you want:
VB.NET:
Dim sb As New System.Text.StringBuilder
For Each entry As DictionaryEntry In e.Keys
    sb.AppendFormat("record {0} of {1} changed:<br/>", entry.Value, entry.Key)
    sb.AppendLine()
Next
For Each entry As DictionaryEntry In e.NewValues
    If entry.Value <> e.OldValues(entry.Key) Then
        sb.AppendFormat("column {0}: '{1}' changed to '{2}'<br/>", entry.Key, e.OldValues(entry.Key), entry.Value)
        sb.AppendLine()
    End If
Next
Dim message As String = sb.ToString()
I included the Html linebreaks, you can set mailmessage.IsBodyHtml=True.
 
Back
Top