?
Solved

Binding results of query to repeater control

Posted on 2005-05-09
2
Medium Priority
?
247 Views
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">
                  Category:
                  <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)
                    conPubs.Open()
                    dtrAuthors = cmdSelect.ExecuteReader()

                    ' Bind to Repeater
                    Repeater1.DataSource = dtrAuthors
                    Repeater1.DataBind()

                    dtrAuthors.Close()
                    conPubs.Close()

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

                    ' Bind to Repeater
                    Repeater1.DataSource = dtrAuthors
                    Repeater1.DataBind()

                    dtrAuthors.Close()
                    conPubs.Close()
...
(repeated for each data element)                                

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


</asp:repeater></p>
0
Comment
Question by:dprasad
  • 2
2 Comments
 
LVL 14

Expert Comment

by:amyhxu
ID: 13965267
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.

0
 
LVL 14

Accepted Solution

by:
amyhxu earned 2000 total points
ID: 13965288
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

840 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