Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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:…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

805 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