3

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

Dropdown:

<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?

3 Answers 3

2

Please 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 the SaleIDDropDown.DataBind(); command. That must refresh the data, but I guess you’re not doing this in correct place, or something like that...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I thought I had solved the problem by disabling ViewState, but I was creating about 10 more problems in the process. The problem ended up being that my DataBind was in Page_Load, so it would bind the data and then do the event that deleted the item from the Dropdown. I needed to another DataBind in the event handler for the delete button.
1

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.

Comments

0

From a now-deleted answer by the question author:

I fixed this by turning off viewstate for the SqlDataSource control, ie setting EnableViewState="False" in the SqlDataSource markup.

This fixed the problem, I guess the data was being updated by the old ViewState data was getting in the way. Try this out if you have the same problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.