DropDownList and Stored Procedure

Posted on 2009-02-16
Last Modified: 2013-11-26
Hi Experts,
i have a stored procedure which i am using to provide search feature in my website. i am passing the value the user enters in textbox to the SP and displaying the results in GridView. one of the field in the GridView is a field called Size. i want this field to be DropDrowList, so that all the sizes of a particular item are displayed there. how can i do this. any please is appreciated . here is my code.

FISRT: aspx page:

<div class="search">

<asp:TextBox id="TextBox1" runat="server"  />

<asp:Button ID="Button1" runat="server" Text="search" OnClick="Button1_Click" />

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySiteConn %>" 

 SelectCommand="MyProcedure" SelectCommandType="StoredProcedure" OnSelecting="SqlDataSource1_Selecting">


                    <asp:Parameter  Name="searchwords"  Type="String" />




        <br />

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"



                <asp:BoundField DataField="ItemNumber" HeaderText="ItemNumber" SortExpression="ItemNumber" />

                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />

                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />

                <asp:BoundField DataField="Size" HeaderText="Name" SortExpression="Size" />



---------------------CODE BEHIND------------------------------

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)


               e.Command.Parameters["@searchwords"].Value = TextBox1.Text;


        protected void Button1_Click(object sender, EventArgs e)




Open in new window

Question by:niceoneishere
    LVL 15

    Assisted Solution

    Convert the boundfield Size to a template field, and then edit the template.  Add a dropdownlist and either add the other sizes manually, or bind it to a datasource that contains the additional sizes.
    LVL 1

    Author Comment

    my question is how to bind the dropdownlist to the stored procedure to the sizes of the items are that being returned by the stored procedure when a user searches. ( all the sizes are stored in the database and they are different from one item to another ) so i dont think i can add them manually.
    i hope i am clear in explaining my problem. i do know how to convert the bound field to a template field and add a ddl.
    Thanks i appreciate it.
    LVL 15

    Assisted Solution

    Ok, so each dropdownlist will have different values?

    I'm thinking the best way to do that would be to convert it to a template, then add code to the RowDataBound function.  Something like this:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
       If e.Row.RowType = DataControlRowType.DataRow Then
          Dim ddl As DropDownList = e.Row.FindControl("DropDownList1")
          'add some code to get data from Stored Procedure
          For Each item In DataRows
             Dim li As New ListItem(item)
       End If
    End Sub

    Open in new window

    LVL 20

    Expert Comment

    Can u show how the data is being fetched from the stored procedure.

    How are you planning to return all the sizes for the record?
    LVL 1

    Accepted Solution

    Hello All,
    i just did it by creating a separate stored procedure which would get me just the item number and sizes and used this as the data source for the DDL.
    Thanks guys i appreciate your help.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    755 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

    19 Experts available now in Live!

    Get 1:1 Help Now