下拉列表不会从sql数据库填充值

时间:2010-10-06 03:20:36

标签: asp.net

我一直在努力解决这个问题。我有一个申请表:

  <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),下拉列表控件将填充数据库中的值,但是当我离开这些值时,它不会填充数据库中的任何值。

当我删除

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

并离开([UserId] = @UserId),我收到错误:

  

必须声明标量变量“@UserId”。

有人可以帮帮我吗? 提前致谢

2 个答案:

答案 0 :(得分:1)

我发布的here in your previous thread肯定有效。

我想您可能错过了网址中的“ID”查询字符串。

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

如果您未指定“?ID=12”,则会抛出错误。

alt text

答案 1 :(得分: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();
}