获取查询结果两次

时间:2011-04-14 14:02:40

标签: .net sqldatareader

您好 我在这个页面得到两次结果,我不知道为什么。当我在DropdownList1中选择选项2后查询数据库时,结果会出现两次。是因为结果出现在updatepanel中吗?非常感谢您的帮助。我无法真正看到代码的问题,这让我很生气!

page.aspx

   <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="Select">Select</asp:ListItem>
 <asp:ListItem value="Option 1">Option 1</asp:ListItem>
   <asp:ListItem Value="Option 2">Option 2</asp:ListItem>
</asp:DropDownList>


   <asp:UpdatePanel id="FirstPanel" runat="server" RenderMode="Inline">
   <ContentTemplate>   
      <div id="tohide1" visible="False" runat="server"> 
           +++Do something+++
           </div>

<div id="tohide2" visible="false" runat="server"> 
    <asp:Label ID="Label1" runat="server"></asp:Label>
   </div>

   </ContentTemplate>
   <Triggers>
   <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
    </Triggers>
    </asp:UpdatePanel>

page.aspx.vb

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    If DropDownList1.SelectedValue = "Option 1" Then

        tohide1.Visible = "True"
        tohide2.Visible = "False"

    ElseIf DropDownList1.SelectedValue = "Option 2" Then

        tohide1.Visible = "False"
        tohide2.Visible = "True"

     Dim connectionString As String = ConfigurationManager.ConnectionStrings("myConnString").ToString()
        Dim SqlQuery As String

        SqlQuery = "SELECT CourseCode, Q1 FROM Courses WHERE (Q1 = 1)"

        Dim conn As SqlConnection = New SqlConnection(connectionString)
        Dim Cmd1 As SqlCommand = New SqlCommand(SqlQuery, conn)

        Try
            conn.Open()

            Dim myReader As SqlDataReader = Cmd1.ExecuteReader


            While myReader.Read()

                Dim CourseCode As String = myReader("CourseCode")

                Label1.Text += CourseCode & "<br />"

          End While

        Catch ex As SqlException
            lblmessage.Text = ex.Message()
        Finally
            conn.Close()
        End Try

    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我会将Label1.Text重置为程序顶部附近的“”。这应该可以阻止结果中的重复。