2013-09-01 71 views
0

我想ddlCity下拉列表中選擇的值傳遞到EntityDataSource2的CommandText其中「WHERE p.city = @city」但我得到以下錯誤:如何通過DropDownList中的SelectedItem到EntityDataSource選擇屬性

WhereParameters cannot be specified unless AutoGenerateWhere==true or Where is specified.

<tr id="SearchDropDown">  
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=medicaldb2Entities" 
    DefaultContainerName="medicaldb2Entities" EnableFlattening="False" 
    EntitySetName="Medicals" Select="it.[medicalName], it.[city], it.[Region]"> 
</asp:EntityDataSource>  
<td> 
<td> 
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" 
onselectedindexchanged="ddlCity_SelectedIndexChanged" 
    DataSourceID="EntityDataSource1" DataTextField="city" 
    DataValueField="city"> 
</asp:DropDownList> 
</td> 
</tr> 
</table>   

<asp:ListView ID="ListView1" runat="server" DataSourceID="EntityDataSource2"> 
<LayoutTemplate> 
      <table> 
       <tr>  
        <td> 
         City 
        </td> 

       </tr> 
       <asp:PlaceHolder ID="ItemPlaceHolder" runat="server"></asp:PlaceHolder> 
      </table> 
     </LayoutTemplate> 
     <ItemTemplate> 
      <tr> 
       <td> 
        <%# Eval("city") %> 
       </td> 
      </tr> 
     </ItemTemplate> 

     <EmptyDataTemplate> 
      Not Found... 
     </EmptyDataTemplate> 

</asp:ListView> 

<asp:EntityDataSource ID="EntityDataSource2" runat="server" 
    CommandText="SELECT p.MedicalID, p.medicalName, p.city, p.Region, p.description, p.Image,  p.adress 
      FROM Medicals AS p WHERE p.city = @city" 
      AutoGenerateWhereClause="False" 
    ConnectionString="name=medicaldb2Entities" 
    DefaultContainerName="medicaldb2Entities"> 

<WhereParameters> 
    <asp:ControlParameter ControlID="ddlCity" DbType="String" 
     DefaultValue="2500" Name="city" PropertyName="SelectedValue" 
    ConvertEmptyStringToNull="true" /> 
    </WhereParameters> 
</asp:EntityDataSource> 

任何幫助,將不勝感激

+1

你可以看看這個教程[教程:實體數據源控件](http://blogs.msdn.com/b/adonet/archive/2008/06/18/tutorial-entity-data-source -control.aspx)。我相信你會得到你的答案。 – afzalulh

+0

感謝它看起來像一個有用的教程,我必須讀它 – mctuna

回答

1

您可以使用

<asp:EntityDataSource 
.... 
    <WhereParameters> 
     <asp:ControlParameter ControlID="costLimit" DbType="Int32" 
      DefaultValue="2500" Name="ordercost" PropertyName="SelectedValue" 
     ConvertEmptyStringToNull="true" /> 
     </WhereParameters> 
..... 
</asp:EntityDataSource> 
+0

我已經嘗試過,我已經改變它爲ControlID =「ddlCity」和名稱=「城市」。然後我再次將查詢寫爲p.city = @City,但它不起作用 – mctuna

+0

我想你的代碼中有語法錯誤。您可以閱讀Msdn的語法或發佈您的代碼 – tdelepine

+0

我粘貼了整個代碼 – mctuna

1

這是爲了在EntityDataSource的Select屬性中傳遞下拉列表selectedvalue的正確版本。

<tr id="SearchDropDown">  
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=medicaldb2Entities" 
    DefaultContainerName="medicaldb2Entities" EnableFlattening="False" 
    EntitySetName="Medicals" Select="it.[medicalName], it.[city], it.[Region]"> 
</asp:EntityDataSource>  
<td> 
<td> 
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" 
onselectedindexchanged="ddlCity_SelectedIndexChanged" 
    DataSourceID="EntityDataSource1" DataTextField="city" 
    DataValueField="city"> 
</asp:DropDownList> 
</td> 
</tr> 
</table> 



<asp:ListView ID="ListView1" runat="server" DataSourceID="EntityDataSource2"> 

<LayoutTemplate> 
      <table> 
       <tr> 
        <td> 
         City 
        </td> 

       </tr> 
       <asp:PlaceHolder ID="ItemPlaceHolder" runat="server"></asp:PlaceHolder> 
      </table> 
     </LayoutTemplate> 
     <ItemTemplate> 
      <tr> 
       <td> 
        <%# Eval("city") %> 
       </td> 
      </tr> 
     </ItemTemplate> 

     <EmptyDataTemplate> 
      not found... 
     </EmptyDataTemplate> 

</asp:ListView> 

<asp:EntityDataSource ID="EntityDataSource2" runat="server" 
ConnectionString="name=medicaldb2Entities" 
    DefaultContainerName="medicaldb2Entities" 
EntitySetName="Medicals" 
    Select= "it.[MedicalID], it.[medicalName], it.[city], it.[Region], it.[description], it.[Image], it.[adress]" 
    Where = "it.[city] = @city"> 

    <WhereParameters> 
    <asp:ControlParameter ControlID="ddlCity" DbType="String" 
     DefaultValue="2500" Name="city" PropertyName="SelectedValue" 
    ConvertEmptyStringToNull="true" /> 
    </WhereParameters> 

</asp:EntityDataSource> 
相關問題