Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1247
  • Last Modified:

DropDownList and Stored Procedure

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">
                 <SelectParameters>
                    <asp:Parameter  Name="searchwords"  Type="String" />
                </SelectParameters>
           </asp:SqlDataSource>
        
        <br />
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
 AutoGenerateColumns="False">
            <Columns>
                <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" />
            </Columns>
        </asp:GridView>
 
---------------------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)
        {
            GridView1.DataBind();
        }

Open in new window

0
niceoneishere
Asked:
niceoneishere
  • 2
  • 2
3 Solutions
 
aibusinesssolutionsCommented:
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.
0
 
niceoneishereAuthor Commented:
Hello,
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.
0
 
aibusinesssolutionsCommented:
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)
         ddl.Items.Add(li)
      Next
   End If
End Sub

Open in new window

0
 
informaniacCommented:
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?
0
 
niceoneishereAuthor Commented:
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.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now