Solved

Running a stored procedure when selecting a row in a gridview

Posted on 2008-10-08
5
151 Views
Last Modified: 2013-11-26
I have a grid view which displays a list of events what I want to do is click the select button to select the row and then this will run a stored procedure to add the currently logged on person to the event attendance table.  I have got so far but I am unsure how to pass the eventID to the stored procedure as a parameter.

Below is the code so far I need someone to tell me what I need to do to complete the Private Sub GrdTrainEvents_RowCommand section below:-

1. a module with database function

Public Sub pageAccessed(ByVal eventID As String)
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings(("ConnectionString")).ConnectionString)
        Dim sqlStr As String = "spInsertEventsAttendance"
        Dim sqlCommand As New SqlCommand(sqlStr, conn)

        sqlCommand.CommandType = CommandType.StoredProcedure
        sqlCommand.Parameters.Add("@eventID", SqlDbType.Char, 10).Value = eventID
        sqlCommand.Parameters.Add("@userID", SqlDbType.Char, 10).Value = userID

        conn.Open()
        sqlCommand.ExecuteNonQuery()
        conn.Close()
    End Sub

2. the code page behind the page contains the following

Private Sub loadingPage()
        Dim UserID As Integer

        UserID = Right(User.Identity.Name, 6)

        ds_QLP_Events.SelectParameters(0).DefaultValue = UserID
        GrdTrainEvents.DataBind()

    End Sub

    Private Sub GrdTrainEvents_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GrdTrainEvents.RowCommand
        If e.CommandName = "select" Then

            DatabaseFunctions.pageAccessed()
        End If
    End Sub

0
Comment
Question by:WNottsC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 22670375
You can make sqlStr = "EXECUTE spInsertEventsAttendance @EventID = " & eventid & ", @UserID = " & userid
Then you don't have to use the command parameters
0
 
LVL 7

Accepted Solution

by:
technofile earned 500 total points
ID: 22670585
      You need to create a SQLParameter object and then add that. Try This.


         Dim sqlStr As String = "spInsertEventsAttendance"
        Dim sqlCommand As New SqlCommand(sqlStr, conn)
        Dim param As New SqlParameter
        sqlCommand.CommandType = CommandType.StoredProcedure

        param = New SqlParameter("@eventID", SqlDbType.Char, 10)
        param.Value = eventID
        sqlCommand.Parameters.Add(param)
        param = New SqlParameter("@userID", SqlDbType.Char, 10)
        param.Value = eventID
        sqlCommand.Parameters.Add(param)


        conn.Open()
        sqlCommand.ExecuteNonQuery()
        conn.Close()

0
 
LVL 7

Expert Comment

by:technofile
ID: 22670610

typo...
        param = New SqlParameter("@userID", SqlDbType.Char, 10)
        param.Value = userID <----{not eventID again}
        sqlCommand.Parameters.Add(param)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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