Solved

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

Posted on 2008-10-13
3
286 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
[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
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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