views:

384

answers:

1

Hi

I thought calling ObjectDataSource.Select() gives the same results as calling ObjectDataSource.DataBind(), but in some cases that doesn’t seem to be true:


<asp:ObjectDataSource ID="ODS1" TypeName="PersonDB" SelectMethod="GetPeople" 
        runat="server"></asp:ObjectDataSource>

<br>

<asp:ListBox ID="ListBox1" DataSourceID="ODS1" DataTextField="PersonID" 
        AutoPostBack="true" runat="server"></asp:ListBox>

<br>

 <asp:ObjectDataSource ID="ODS2" InsertMethod="InsertEmployee" 
        TypeName="PersonDB" SelectMethod="GetPerson" runat="server"> 
           <SelectParameters>
              <asp:ControlParameter ConvertEmptyStringToNull="True" Name="PersonID"
                     PropertyName="SelectedValue" ControlID="ListBox1" />
           </SelectParameters>
 </asp:ObjectDataSource>

 <br>

 <asp:DetailsView ID="DetailsView1" AutoGenerateInsertButton="true" DataSourceID="ODS2"
        runat="server"> </asp:DetailsView>


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ODS1.Select(); //same as calling ODS1.DataBind();
    }

    if (IsPostBack)
    {
        ODS2.Select(); // returns no results
    }
}


In the above code calling ODS1.Select() produces the same results as calling ODS1.DataBind(). But if on postback user selects an item in ListBox, ODS2.Select() still doesn’t return any results, but if we replace ODS2.Select(); with ODS2.DataBind(); then a row is returned. So why doesn’t ODS2.Select(); return any results, but ODS2.DataBind(); does?


thank you


EDIT:


Assuming user select an item in a Listbox --> It seems that when we call ODS2.Select(), ODS2 for some reason can't bind to ListBox1.SelectedValue and extract a value from this property

+1  A: 

ODS2 has a Select parameter, which in your sample page load is bound to a ListBox control that hasn't been databound. What's in the ListBox? What is being passed in the PersonID parameter that is passed to ODS2?

The most obvious way to start answering your question would be to set a breakpoint in the GetPerson method of PersonDB and see what is being passed as parameters. Then follow the code to see what gets retrieved.

Cylon Cat
the first time page is created, DetailsView won't display any results, but when user selects an item in a ListBox, ODS2 should ( and it does when calling ODS2.DataBind()) return results.
carewithl
How do you handle the case when there's nothing selected in the list box? That may be the case when you first load.
Cylon Cat
True, on first load DetailsView doesn't display anything, but on postback it should, even if we call ODS2.Select( assuming user selects an item in ListBox )
carewithl