dropdown list wont populate values from sql database
I've been strugglng with this one problem for a while now. I have an application as:
<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:
<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" Nam开发者_如何学JAVAe="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>
When i take away the
<SelectParameters>
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>
and ([UserId] = @UserId)
, the dropdown list control will populate values from the database but when I leave those 2, it won't populate any value from the database.
When I remove the
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>,
and leave ([UserId] = @UserId)
, I get the error:
Must declare the scalar variable "@UserId".
Can somebody please help me out? Thanks in advance
The one I posted here in your previous thread is definitely working.
I think you might have missed out the "ID" querystring in your url.
Are you browsing to "http://localhost:1234/WebAppName/PageName.aspx?=12
"? (assume the auto generated port number is 1234 and the ID is 12)
It will throw error if you don't specify "?ID=12
".
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();
}
精彩评论