Solved

Running a stored procedure when selecting a row in a gridview

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search an image for an image 3 30
Disable extension 8 38
VB.Net - Get ColumnNames of SQL Table 4 11
ASP/VB email question 4 34
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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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