Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
I have an application in asp.net/vb.net and sql server express 2008. On the form i have two dropdownlists one for selection of years(from 2006-2013), the other is selection of grades(from 1-8). The other controls are listbox and a button. I want the results to be displayed in listbox upon selecting year and grade from dropdownlists and on click of a button. The select query works great in sql server. But in the application it is doing what i want except for year 2006 and 2013 but data for these years is there. Iam using dropdown selected value. Why the query is not selecting records for the first and last item in a dropdownlist.

the code below

VB
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim queryString As String = _
                "SELECT DISTINCT Code FROM enrolnment WHERE grade ='" & DropDownList2.Selectedvalue & "' AND Year ='" & DropDownList3.Selectedvalue & "'AND iActive = '1';"
        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand(queryString, connection)
            connection.Open()
            Dim reader As SqlDataReader = command.ExecuteReader()
            Dim i As Integer = 0
            command.CommandTimeout = 300

            While reader.Read()
                'i < Listbox1.Items.Count
                ListBox1.Items.Add(reader.GetValue(i).ToString())
                command.CommandTimeout = 300
            End While
            reader.Close()

            'End Try
        End Using

    End Sub
Posted
Updated 17-Aug-13 23:26pm
v4
Comments
Sergey Alexandrovich Kryukov 18-Aug-13 3:41am    
Oh, it does selecting records correctly, unless you screw it up with some weird technique. What is it in your case? You should know better. Due to the weather condition, my access to your hard drive is somewhat limited.
—SA
OriginalGriff 18-Aug-13 3:49am    
What Sergey means is "There are a lot of ways you could be accessing the info, and we don't know which it is"
Without your code to show us exactly how you are using the selected value, there isn't a lot we can do - we can't see your screen, or access your HDD so we only get to work with what you tell us.
Use the "Improve question" widget to edit your question, and provide better information!
mayeso 18-Aug-13 4:39am    
code now in the question, i forgot to include it
ridoy 18-Aug-13 4:19am    
where is your code,show us here.
mayeso 18-Aug-13 4:38am    
code now in the question

1 solution

For Anyone still having the problem; I solved it in a different, yet easier way: Just add a dummy ListItem to the start of the DropDownList and set that item's Enabled property to false. i.e


<asp:dropdownlist id="dropdown1" runat="server" autopostback="true" onselectedindexchanged="dropdown1_SelectedIndexChanged" viewstatemode="Enabled" xmlns:asp="#unknown">
<asp:listitem value="" text="" enabled="false" />
<asp:listitem value="1" text="Item 1" />
<asp:listitem value="2" text="Item 2" />
<asp:listitem value="3" text="Item 3" />
<asp:listitem value="4" text="Item 4" />
<asp:listitem value="5" text="Item 5" />
 
Share this answer
 
v2
Comments
mayeso 18-Aug-13 5:25am    
Thanks Arun, what if the dropdownlists are bound to data table fields? they getting the values from a database

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900