2010-10-06 143 views
1

我一直在努力解决这个问题一段时间了。我有一个应用程序为:下拉列表将不会从sql数据库填充值

<asp:SqlDataSource runat="server" ID="categoriesDataSource" 
    SelectCommand="SELECT [CategoryID], [Name], [UserId] FROM [Categories] WHERE ([UserId] = @UserId) ORDER BY [Name]"> 
    <SelectParameters> 
     <asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" /> 
    </SelectParameters> 
    </asp:SqlDataSource> 

    Pick a category: &nbsp; 
    <asp:DropDownList ID="categories" runat="server" AutoPostBack="True" 
     DataSourceID="categoriesDataSource" DataTextField="Name" 
     DataValueField="CategoryID" AppendDataBoundItems="True"> 
    <asp:ListItem Selected="True" Value="">-- All Categories --</asp:ListItem> 
</asp:DropDownList> 

<asp:SqlDataSource ID="picturesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
      ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
      SelectCommand="SELECT PictureID, Title, UploadedOn 
FROM Pictures 
WHERE UserId = @UserId AND 
(CategoryID = @CategoryID OR @CategoryID IS NULL) 
ORDER BY UploadedOn DESC" CancelSelectOnNullParameter="False"> 
       <SelectParameters> 
        <asp:QueryStringParameter Name="UserId" QueryStringField="ID" /> 
        <asp:ControlParameter ControlID="categories" Name="CategoryID" PropertyName="SelectedValue" 
         Type="Int32" /> 
       </SelectParameters> 
      </asp:SqlDataSource> 
      <br /> 
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
     AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
     DataKeyNames="PictureID" DataSourceID="picturesDataSource" ForeColor="#333333" 
     GridLines="None"> 
     <AlternatingRowStyle BackColor="White" /> 
     <Columns> 
      <asp:HyperLinkField DataNavigateUrlFields="PictureID" 
       DataNavigateUrlFormatString="~/Photodetail.aspx?ID={0}" Text="View Comments" /> 
      <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> 
      <asp:BoundField DataField="UploadedOn" HeaderText="Date Added" 
       SortExpression="UploadedOn" /> 
      <asp:ImageField DataImageUrlField="PictureID" 
       DataImageUrlFormatString="~/UploadedImages/{0}.jpg" HeaderText="Image"> 
       <ControlStyle Width="100px" /> 
      </asp:ImageField> 
     </Columns> 
     <EditRowStyle BackColor="#2461BF" /> 
     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#EFF3FB" /> 
     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
     <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
     <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
     <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
     <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
    </asp:GridView> 

当我带走

<SelectParameters> 
    <asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" /> 
</SelectParameters> 

([UserId] = @UserId),下拉列表控件将填充从数据库中值,但是,当我离开的2,它不会填充来自数据库的任何值。

当我删除了

<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" /> 
    </SelectParameters>, 

离开([UserId] = @UserId),我得到的错误:

Must declare the scalar variable "@UserId".

有人可以请帮我吗? 在此先感谢

+0

认真伙计,你已经张贴在过去的几天里4点相同的问题。 – RPM1984 2010-10-06 03:21:43

+0

是啊我一直在寻找解决问题的方法,但我还没有找到一个 – onfire4JesusCollins 2010-10-06 03:34:56

+0

,所以发布更新到一个问题,你有什么试过等等,开放新的重复问题不会帮助,实际上它会挫败社区。 – RPM1984 2010-10-06 03:38:09

回答

1

我张贴here in your previous thread绝对工作。

我想你可能错过了URL中的“ID”查询字符串。

你正在浏览的是“http://localhost:1234/WebAppName/PageName.aspx?=12”吗? (假设自动生成的端口号是1234,ID是12)

如果您未指定“?ID=12”,它将引发错误。

alt text

0
public void bind() 
{ 
    sqldataadapter da=new sqldataadapter("select A,B from table",cn); 
    datatable dt=new datatable(); 
    da.fill(dt); 
    dropdownlist1.datatextfield="A"; 
    dropdownlist1.datavaluefield="B"; 
    dropdownlist1.datasource=dt; 
    dropdownlist1.databind(); 
} 
相关问题