• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

Gridview and column updates

I have an email which sucessfully fires when a row is edited\updated in a gridview, however it fires on the editing of anything in that current selected row.
How I can make it fire an email when say for instance, a column that contains the field status is set to 'complete', basically rather than firing once the row is updated, I would like to be a bit more specific.

Ideally, I want an email to be sent once the task has been 'Assigned' and a different email fired when a task has been 'Completed'

Any ideas ?
  • 6
  • 5
1 Solution
Obadiah ChristopherCommented:
Please show the code for gridview updating.
ndr-itsolutionsAuthor Commented:
 Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
        ' Indicate whether the update operation succeeded.
        If e.Exception Is Nothing Then
            Dim strEmail As String
            strEmail = String.Empty
            Dim Resource As String = e.NewValues(5).ToString()
            Dim strEnv As String = e.NewValues(2).ToString()
            Dim strBuild As String = e.NewValues(3).ToString()
            Dim strComponent As String = e.NewValues(1).ToString()
            Dim strSubComponent As String = e.NewValues(4).ToString()
            Dim strResource As String = e.NewValues(5).ToString()
            Dim strComments As String = e.NewValues(10).ToString()
            Dim strDate As String = e.NewValues(7).ToString()
            Dim strDoc As String = e.NewValues(15).ToString()
            Dim strSoft As String = e.NewValues(12).ToString()
            Dim strID As String = e.NewValues(0).ToString()

            'Retrieve email address
            Dim cm As New SqlCommand("SELECT Res_Email FROM Resource_ref_Data WHERE Resource = @FirstName")
            Dim param As New SqlParameter("@FirstName", Resource)
            Dim cn As New SqlConnection("Data Source=server1;Initial Catalog=FFF;Persist Security Info=True;User ID=users;Password=xxxxxx")
            cm.Connection = cn
            Dim oResult As Object
            If cn.State = ConnectionState.Closed Then
            End If
            oResult = cm.ExecuteScalar()
            If (Not oResult Is Nothing) Then
                strEmail = oResult.ToString()
            End If
            SendMailResource(strEmail, strEnv, strBuild, strComponent, strSubComponent, strResource, strComments, strDate, strDoc, strSoft, strID)
            e.ExceptionHandled = True
            Response.Write("An error occurred while attempting to update the row. No email sent.")

        End If
    End Sub

    Private Function currentRow() As Object
        Throw New NotImplementedException
    End Function

    Protected Sub SendMailResource(ByVal strEmailAddress As String, ByVal strEnv As String, ByVal strBuild As String, ByVal strComponent As String, ByVal strSubComponent As String, ByVal strResource As String, ByVal strComments As String, ByVal strDate As String, ByVal strDoc As String, ByVal strSoft As String, ByVal strID As String)
        Dim mail As New MailMessage()
        'set the addresses

        Dim sEmailBody As String
        sEmailBody = My.Resources.Resource1.Resource_Email

        sEmailBody = sEmailBody.Replace("[strID]", strID)
        sEmailBody = sEmailBody.Replace("[strEnv]", strEnv)
        sEmailBody = sEmailBody.Replace("[strBuild]", strBuild)
        sEmailBody = sEmailBody.Replace("[strComponent]", strComponent)
        sEmailBody = sEmailBody.Replace("[strSubComponent]", strSubComponent)
        sEmailBody = sEmailBody.Replace("[strResource]", strResource)
        sEmailBody = sEmailBody.Replace("[strComments]", strComments)
        sEmailBody = sEmailBody.Replace("[strDate]", strDate)
        sEmailBody = sEmailBody.Replace("[strDoc]", strDoc)
        sEmailBody = sEmailBody.Replace("[strSoft]", strSoft)

        mail.From = New MailAddress("test@test.com")
        'set the content
        mail.Subject = "TEST"
        mail.Body = sEmailBody
        mail.IsBodyHtml = True
        'send the message
        Dim smtp As New SmtpClient("relay.server")
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    End Sub

Open in new window

>Dim Resource As String = e.NewValues(5).ToString()

You are using the NewValues collection. You can similarly use the OldValues collection so that you can do some checks. For example

If e.OldValues(IndexOfStatusColumn) = "Unassigned" And e.NewValues(IndexOfStatusColumn)  = "Assigned" Then
     'Code to send the assigned email
Else If e.OldValues(IndexOfStatusColumn) = "Assigned" And e.NewValues(IndexOfStatusColumn)  = "Completed" Then
     'Code to send the completed email
End If
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

ndr-itsolutionsAuthor Commented:
If the value does not show 'UnAssigned' it will show the name of the resource, how would that work with that code you have supplied ?

Same with 'Completed' other choices are 'Not Deployed' or 'In Progress'

I presume I would insert this code just below :

Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
Yes code will go in RowUpdated event.

You can modify those if conditions to work the way you want.
ndr-itsolutionsAuthor Commented:
I'm not sure about the 'if' statement, would I use not equal to if I have multiple resources to choose from?
ndr-itsolutionsAuthor Commented:
I can't seem to get this working at all.
What would be the possible values of status?

Name Of Some Resource
In Progress
Not Deployed

Is that correct?
ndr-itsolutionsAuthor Commented:
So the values for the status column are:
In progress
Not Started
Not deployed

And the values in the resource column are

Nick Smith
Andy Smith
Another person
Ben Smith
You need to put if conditions based on your workflow. So if the workflow is

Not Started->In Progress->Not Deployed->Raised->Completed

And you need to send emails at each status change then you can put a condition like below

If e.OldValues(statuscolumnindex) = "Not Started" AND e.NewValues(statuscolumnindex)="In Progress" Then
   'Send email
Else If e.OldValues(statuscolumnindex) = "In Progress" AND e.NewValues(statuscolumnindex)="Not Deployed" Then
   'Send another email
End If
ndr-itsolutionsAuthor Commented:
So i would add that condition under the Rowupdated sub and copy all subsequent code into where you have commented 'Send Email ?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now