Solved

Reading values from a Gridview

Posted on 2013-01-31
8
344 Views
Last Modified: 2013-01-31
I am building a small web app for managing tasks and I have the following code which lets me Edit a row in gridview and then select a resource, when I update that row the code then looks up the resources email address and then send an email with the details of what was in that row. My issue is the last part, how can I read all the other information in that row and assign to variables ?
I used e.NewValues(5).ToString() to retrieve the resource name, but need to retrieve the other row details to be included in the email.

Any ideas ?



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()

            '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=EMS;Persist Security Info=True;User ID=user;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)
        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)
        Dim mail As New MailMessage()
        'set the addresses

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

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


        mail.From = New MailAddress("test@test.com")
        mail.To.Add(strEmailAddress)
        'set the content
        mail.Subject = "Task Awaiting"
        mail.Body = sEmailBody
        mail.IsBodyHtml = True
        'send the message
        Dim smtp As New SmtpClient("smtp1")
        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
Comment
Question by:ndr-itsolutions
  • 4
  • 4
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38839183
>I used e.NewValues(5).ToString() to retrieve the resource name

Use

e.NewValues(IndexOfOtherColumns)

to get values for other columns.
0
 

Author Comment

by:ndr-itsolutions
ID: 38839403
I tried that and it did not work, could be my coding though, as I am new to this.

So I added the following to the gridview sub routine:

Dim strEnv As String = e.NewValues(1) 

Open in new window


and

sEmailBody = sEmailBody.Replace("[strEnv]", strEnv)

Open in new window


to the SendMail Resource routine and got the following message, stating strEnv was not declared ??
Error Message
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38839416
Is the variable in the same sub as the email body code? Otherwise, that variable cannot be seen. And this issue is not related to gridview.
0
 

Author Comment

by:ndr-itsolutions
ID: 38839430
Not it's not in the same sub, what's the best way to fix this issue ? Put the email sub into the gridview sub ?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38839492
Or pass that value into email sub.
0
 

Author Comment

by:ndr-itsolutions
ID: 38839514
Do you have an example of the code I would use to pass the values I need ?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38839587
Just like you pass the email address.


Change

Protected Sub SendMailResource(ByVal strEmailAddress As String)

to

Protected Sub SendMailResource(ByVal strEmailAddress As String, ByVal strEnv As String)


and

SendMailResource(strEmail)

to

Dim strEnv As String = e.NewValues(1)
SendMailResource(strEmail, strEnv)
0
 

Author Closing Comment

by:ndr-itsolutions
ID: 38839788
That worked perfectly
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syntax Error 2 43
What .NET URL re-routing tool did I use? 2 34
Error in JQuery 5 36
Handle KeyDown for ToolStripDropDownButton, ToolStripMenuItem 3 18
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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 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

12 Experts available now in Live!

Get 1:1 Help Now