SQL Server SqlDataSource not reselecting data from database when data is changed

ltqd579y  于 2023-10-15  发布在  其他
关注(0)|答案(2)|浏览(113)

I have a Dropdown list being populated by an SqlDataSource, with the following markup:

<asp:DropDownList ID="SaleIDDropDown" runat="server" CssClass="dropdown" 
    DataSourceID="DropDownDataSource" DataTextField="Title" DataValueField="ID"
    AppendDataBoundItems="true">
</asp:DropDownList>

DataSource

<asp:SqlDataSource ID="DropDownDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SQL-DEV %>" 
    SelectCommand="SELECT [ID], [Title], [userid] FROM [AdSelect]">
</asp:SqlDataSource>

The problem I'm running into is when some of the data is changed via the web interface I'm making (I delete an item, or change it's Title for example) the Dropdown populates with the old data, even though it clearly has changed in the DataBase (I checked using SQL Server Studio). If I reload the page, then the Dropdown is finally populated correctly.

I need a way to "force" a refetching/selecting of the data, but my efforts have so far failed. I've tried doing it in code, but it's not taking effect.

DropDownDataSource.Select(DataSourceSelectArguments.Empty);
DropDownDataSource.DataBind();

SaleIDDropDown.DataBind();

It still doesn't update the dropdown, even when this is run.

How can I make the dropdown and its SqlDataSource and push it to the page on command?

p5fdfcr1

p5fdfcr11#

Pls note that even though your solution with disabling viewstate works it is far from optimal because it will load the data from database on every postback.

If it’s a busy website you don’t really want that. Try spending some more time on SaleIDDropDown.DataBind(); command. That must refresh the data but I guess you’re not doing this in correct place or something like that…

unguejic

unguejic2#

I've also found that if the SQLDataSource control has CancelSelectOnNullParameter="True" (the default) and any the Select Parameter has ConvertEmptyStringToNull="true" and the parameter is blank .. then query won't run. Sounds obvious but it's tripped me up.

相关问题