Binding results of query to repeater control

Posted on 2005-05-09
Last Modified: 2010-04-07
After a selection a selection is made, the form posts to the server, but no data gets displayed in the repeater. no errors are given.

<form id="Form1" runat="server">
                  <asp:dropdownlist id="category" runat="server" autoPostBack="True">
                        <asp:ListItem Text="" />
                        <asp:ListItem Text="Barbera" />
<asp:dropdownlist id="region" runat="server">
                        <asp:ListItem Text="" />
                        <asp:ListItem Text="Aconcagua" />

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Put user code to initialize the page here

            If Not IsPostBack Then
                Dim conPubs As SqlConnection
                Dim cmdSelect As SqlCommand
                Dim dtrAuthors As SqlDataReader

                ' Retrieve records from database
                conPubs = New SqlConnection("Server=localhost;Trusted_Connection=true;Database=winestore")
                If color.SelectedItem.Text <> "" Then
                    cmdSelect = New SqlCommand("Select * From maintable where color=" & color.SelectedItem.Text, conPubs)
                    dtrAuthors = cmdSelect.ExecuteReader()

                    ' Bind to Repeater
                    Repeater1.DataSource = dtrAuthors


                ElseIf category.SelectedItem.Text <> "" Then
                    cmdSelect = New SqlCommand("Select * From maintable where category=" & category.SelectedItem.Text, conPubs)
                    dtrAuthors = cmdSelect.ExecuteReader()

                    ' Bind to Repeater
                    Repeater1.DataSource = dtrAuthors

(repeated for each data element)                                

<asp:repeater id="Repeater1" runat="server">
  <%# Container.DataItem( "title" ) %>

Question by:dprasad
    LVL 14

    Expert Comment

    The problem is with your query strings :
    "Select * From maintable where color=" & color.SelectedItem.Text
    "Select * From maintable where category=" & category.SelectedItem.Text

    In SQL you use "Select * From maintable where color = 'Red'" for string comparison.

    So modify your query strings to:
    "Select * From maintable where color = '" & color.SelectedItem.Text.Trim & "'"
    "Select * From maintable where category = '" & category.SelectedItem.Text.Trim & "'"

    Or use parameters in the query:
    "Select * From maintable where color = @color"
    "Select * From maintable where category = @category"
    and you need the code to add parameters to sqlcommand.

    LVL 14

    Accepted Solution

    By the way, if you use integers to represent color and category, your queries become:
    "Select * From maintable where color=" & color.SelectedItem.Value
    "Select * From maintable where category=" & category.SelectedItem.Value

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 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

    18 Experts available now in Live!

    Get 1:1 Help Now