?
Solved

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

Posted on 2008-10-13
3
Medium Priority
?
289 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 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