2012-01-27 20 views
1

私はこのコードを私と一緒に持っていますが、ドロップダウンボックスからSQLクエリを実行しようとしています。テーブルのドロップダウンボックスの結果を表示

<html> 
    <body> 
     <form id="form1" runat="server"> 
      <asp:DropDownList ID="DropDownList1" runat="server" 
       DataSourceID="SqlDataSource1" DataTextField="a" DataValueField="a"> 
      </asp:DropDownList> 
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
       ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString7 %>" 
       SelectCommand="SELECT DISTINCT [a] FROM [Table_1]"></asp:SqlDataSource> 
      <br /> 
      <br /> 
      <asp:DropDownList ID="DropDownList2" runat="server" 
       DataSourceID="SqlDataSource2" DataTextField="b" DataValueField="b"> 
      </asp:DropDownList> 
      <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
       ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString8 %>" 
       SelectCommand="SELECT DISTINCT [b] FROM [Table_1]"></asp:SqlDataSource> 
      <br /> 
      <br /> 
      <asp:DropDownList ID="DropDownList3" runat="server" 
       DataSourceID="SqlDataSource3" DataTextField="c" DataValueField="c"> 
      </asp:DropDownList> 
      <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
       ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString9 %>" 
       SelectCommand="SELECT DISTINCT [c] FROM [Table_1]"></asp:SqlDataSource> 
      <br /> 
      <br /> 
      <br /> 
      <asp:DropDownList ID="DropDownList4" runat="server" 
       DataSourceID="SqlDataSource4" DataTextField="d" DataValueField="d"> 
      </asp:DropDownList> 
      <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
       ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString10 %>" 
       SelectCommand="SELECT DISTINCT [d] FROM [Table_1]"></asp:SqlDataSource> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
       DataSourceID="SqlDataSource5"> 
       <Columns> 
        <asp:BoundField DataField="a" HeaderText="a" SortExpression="a" /> 
        <asp:BoundField DataField="b" HeaderText="b" SortExpression="b" /> 
        <asp:BoundField DataField="c" HeaderText="c" SortExpression="c" /> 
        <asp:BoundField DataField="d" HeaderText="d" SortExpression="d" /> 
        <asp:BoundField DataField="e" HeaderText="e" SortExpression="e" /> 
        <asp:BoundField DataField="f" HeaderText="f" SortExpression="f" /> 
       </Columns> 
      </asp:GridView> 
      <asp:SqlDataSource ID="SqlDataSource5" runat="server" 
       ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString11 %>" 
       SelectCommand="SELECT [a], [b], [c], [d], [e], [f] FROM [Table_1] WHERE (([a] = @a) AND ([b] = @b) AND ([c] = @c) AND ([d] = @d))"> 
       <SelectParameters> 
        <asp:ControlParameter ControlID="DropDownList1" Name="a" 
         PropertyName="SelectedValue" Type="String" /> 
        <asp:ControlParameter ControlID="DropDownList2" Name="b" 
         PropertyName="SelectedValue" Type="String" /> 
        <asp:ControlParameter ControlID="DropDownList3" Name="c" 
         PropertyName="SelectedValue" Type="String" /> 
        <asp:ControlParameter ControlID="DropDownList4" Name="d" 
         PropertyName="SelectedValue" Type="String" /> 
       </SelectParameters> 
      </asp:SqlDataSource> 
     </form> 
    </body> 
</html> 

小さなSQLテーブルがあり、ドロップダウンボックスから値を選択すると、そのテーブルに結果が表示されます。私はそれをすることができません。何か案が?

+0

コードを少し整理しました。 – jadarnel27

答えて

1

あなたのロジックは健全に見えます。

あなたは変更は、すぐにあなたが各DropDownListの値を変更すると反映させたい場合は、それらの各AutoPostBack="True"プロパティを追加する必要があります。私はそれがあなたのテーブルに記入されなくなることがあります見ることができる唯一の他の事はあなたのGridViewためのSQLクエリがあまりにも制限されている場合である

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="SqlDataSource1" DataTextField="a" DataValueField="a"> 
</asp:DropDownList> 

:例として、これは最初のものは次のようになります。 。おそらく、WHERE句にANDの代わりにORを使用することを意図していましたか?

編集:ここつ以上の思考、それはあなたがあなたのGridViewDropDownList秒のすべてがデータバインドされている前をデータバインドされている可能性があります。これにより、SelectedValuesがまだ存在しないため、ControlParameterGridViewがデータを取得したときに値を持たなくなります。

SqlDataSource5.DataBind(); 
GridView1.DataBind(); 

ヒント:あなたは、あなたのGridViewを再バインドするDropdownListsがデータバインドされている全てれるまで待ってから、このコードを使用する方法を把握する必要がありますチェックし、確認するために、これはあなたの問題であることそのコードを_DataBoundというイベントに入れて、のDropDownListの各イベントをとすることができます。あまり効率的ではありませんが、うまくいくはずです。

+0

出力に変化はありません。/ – Kaushank

+0

@ankurkaushal私はこのasnwerに別の解決策を加えました。 – jadarnel27

+0

@ankurkaushal更新されたお礼をチェックしましたか? – jadarnel27