Solved

Gridview and column updates

Posted on 2013-02-04
12
338 Views
Last Modified: 2013-02-11
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 ?
0
Comment
Question by:ndr-itsolutions
  • 6
  • 5
12 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38852282
Please show the code for gridview updating.
0
 

Author Comment

by:ndr-itsolutions
ID: 38853180
 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)
            cm.Parameters.Add(param)
            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
                cn.Open()
            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)
        Else
            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")
        mail.To.Add("test@test.com")
        mail.To.Add(strEmailAddress)
        'set the content
        mail.Subject = "TEST"
        mail.Body = sEmailBody
        mail.IsBodyHtml = True
        'send the message
        Dim smtp As New SmtpClient("relay.server")
        smtp.Send(mail)
    End Sub
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
       
    End Sub

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38854427
>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
0
 

Author Comment

by:ndr-itsolutions
ID: 38858795
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)
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38859023
Yes code will go in RowUpdated event.


You can modify those if conditions to work the way you want.
0
 

Author Comment

by:ndr-itsolutions
ID: 38859278
I'm not sure about the 'if' statement, would I use not equal to if I have multiple resources to choose from?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ndr-itsolutions
ID: 38859345
I can't seem to get this working at all.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38859639
What would be the possible values of status?

Unassigned
Name Of Some Resource
In Progress
Not Deployed
Completed

Is that correct?
0
 

Author Comment

by:ndr-itsolutions
ID: 38860017
So the values for the status column are:
Completed
In progress
Not Started
Not deployed
Raised

And the values in the resource column are

UnAssigned
Nick Smith
Andy Smith
Another person
Ben Smith
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38863489
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
0
 

Author Comment

by:ndr-itsolutions
ID: 38867557
So i would add that condition under the Rowupdated sub and copy all subsequent code into where you have commented 'Send Email ?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38869176
Yes.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now