Solved

Running a stored procedure when selecting a row in a gridview

Posted on 2008-10-08
5
147 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wpf develop 5 35
Clone Visual studio 2013 solution 5 16
Why is "packages element is not declared"? 2 44
XML & .net 5 21
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

10 Experts available now in Live!

Get 1:1 Help Now