Solved

How to fire a stored procedure on dropdownlist change passin the row from a gridview?

Posted on 2008-10-13
3
278 Views
Last Modified: 2013-11-07
I used the code below to fire a stored procedure without having to 'edit' the row using a command button, it works fine, but now I want to do the same using a dropdownlist which will be in each gridview row, again without having to 'edit' the row.
How do I make it so that when I change one of the drop down lists it passes the row key value and the dropdown value to a stored procedure similarly to this command button solution?
Thanks in advance,
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

        If e.CommandName = "SetMessagePreference" Then

 

            Dim sConnStr As String = SqlDataSource1.ConnectionString

            Dim cnBKTest As New SqlConnection(sConnStr)

            Dim cmdTest As New SqlCommand("FGContactDetailsMessagePreference", cnBKTest)

 

            cmdTest.CommandType = Data.CommandType.StoredProcedure

            cmdTest.Parameters.Add(New SqlParameter("@ContractID", Data.SqlDbType.VarChar, 10))

            cmdTest.Parameters("@ContractID").Value = e.CommandArgument

            cmdTest.Parameters.Add(New SqlParameter("@MessagePreferenceID", Data.SqlDbType.VarChar, 10))

            cmdTest.Parameters("@MessagePreferenceID").Value = 2

 

            cnBKTest.Open()

            cmdTest.ExecuteNonQuery()

            cnBKTest.Close()

            GridView1.DataBind()

        End If

    End Sub

Open in new window

0
Comment
Question by:dgloveruk
3 Comments
 
LVL 12

Accepted Solution

by:
madhevan_pillai earned 500 total points
ID: 22711237
Try this

aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataMember="DefaultView" DataSourceID="SqlDataSource1"   >
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:TemplateField>
            <HeaderTemplate>
            <label>ID1 </label>
            </HeaderTemplate>
            <ItemTemplate >
           
            <asp:DropDownList AutoPostBack="true"    id="ddlEmployeeID1" DataSourceID="SqlDataSource1"
                Runat="Server" DataTextField="Id1" DataValueField="Id1"  SelectedValue='<%#Bind("Id1") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />

            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        </asp:GridView>
       
     
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
            SelectCommand="SELECT [Id], [Name], [Id1] FROM [Parent]"></asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>


codebehind
Partial Class _Default
    Inherits System.Web.UI.Page

  Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim i As Integer
    'i will have the id
    i = CInt(DirectCast(DirectCast(DirectCast(sender, DropDownList).Parent, System.Web.UI.WebControls.DataControlFieldCell).Parent, System.Web.UI.WebControls.GridViewRow).Cells(0).Text)

    ''add code to invoke the stored procedure

  End Sub
End Class
0
 

Author Closing Comment

by:dgloveruk
ID: 31505643
Thanks for that, sorry for the delay in awarding points!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

11 Experts available now in Live!

Get 1:1 Help Now