?
Solved

Running a stored procedure when selecting a row in a gridview

Posted on 2008-10-08
5
Medium Priority
?
154 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 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