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
Solved

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

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

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

861 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