3

This error happens when I use the SelectedItem.Text of a dropdownlist as a value to pass for ObjectDataSource. Here is the markup

<asp:ObjectDataSource ID="odsInsert" runat="server" SelectMethod="GetStudentInClass2" TypeName="MIHE_MIS.DALS.MidTermExamResultDAL">
                <SelectParameters>
                    <asp:ControlParameter DefaultValue="" ControlID="ddlClasses" Name="classCode" PropertyName="SelectedItem.Text" Type="String" />
                    <asp:ControlParameter ControlID="ddlSemesters" Name="semesterID" PropertyName="SelectedValue" Type="Int32" />
                    <asp:ControlParameter ControlID="ddlSpecialization" Name="specializationID" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
            </asp:ObjectDataSource>

Moreover, I add the Select Class text the dropdownlist dynamically.

protected void ddlClasses_DataBound(object sender, EventArgs e)
        {
            ListItem list = new ListItem("Select Class", "-1");
            this.ddlClasses.Items.Insert(0, list);
        }
Elham Kohestani
  • 3,013
  • 3
  • 20
  • 29

1 Answers1

1

If you look at the markup for semesterID, it's binding to SelectedValue which is expected to be Int32. If you now look at the markup for classCode, you're binding to SelectedValue.Text on the same object. We know from the first instance that SelectedValue is Int32 which doesn't have a property called Text. You'll need to correct your binding to the correct object and property type.

Based on the code above, the ObjectDataSource will not be able to pick out the text from SelectedItem.Text. In order to get around this, you need to modify the ObjectDataSource to have a Selecting event like so;

<asp:ObjectDataSource ID="odsInsert" runat="server" SelectMethod="GetStudentInClass2" 
                      TypeName="MIHE_MIS.DALS.MidTermExamResultDAL"
                      OnSelecting="odsInsert_Selecting">
    <SelectParameters>
        <asp:Parameter Name="classCode" Type="String" />
        <asp:ControlParameter ControlID="ddlSemesters" Name="semesterID" PropertyName="SelectedValue" Type="Int32" />
        <asp:ControlParameter ControlID="ddlSpecialization" Name="specializationID" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

Then in your code behind you'd have the event declared;

protected void odsInsert_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["classCode"] = ddlClasses.SelectedItem.Text;
}
DiskJunky
  • 4,750
  • 3
  • 37
  • 66
  • The error is gone but problem still exist. I get no data and that is because of the SelectedItem.Text the objectdataSource can't get the SelectedItem.Text – Elham Kohestani Oct 10 '17 at 11:10
  • You'll need to hook into the `ObjectDataSource`'s `Selecting` event and supply the parameter programatically. You can't bind direct in the manner you need. – DiskJunky Oct 10 '17 at 11:11
  • @ElhamKohestani, I've updated my answer with how to supply `SelectedItem.Text` – DiskJunky Oct 10 '17 at 11:22
  • It is not working. I changed the datasource to a DataTable it worked well but with objectDataSource it is not working don't know. :( – Elham Kohestani Oct 10 '17 at 11:29
  • @ElhamKohestani What part is not working? I actually based the code sample above on code we have in production - it should work. What are you seeing? – DiskJunky Oct 10 '17 at 11:32
  • The object datasource does not contain data hence there is no record in the gridview – Elham Kohestani Oct 10 '17 at 11:34
  • Is the `Selecting` event being hit? Is it triggered after the `DataBound` event (I'm assuming your DDL is also bound)? – DiskJunky Oct 10 '17 at 11:35
  • The select event is being hit too but still not working – Elham Kohestani Oct 10 '17 at 11:38
  • And is there a value in `ddlClasses.SelectedItem.Text` at that moment in time? – DiskJunky Oct 10 '17 at 11:42
  • OK, but if you execute `GetStudentInClass2` in SQL Management Studio and supply it with the same parameters, what do you get? It sounds like the selected parameters don't pick out a student which is why your grid is empty – DiskJunky Oct 10 '17 at 11:48
  • The query works fine and it works when I return the result of that query in a DataTable – Elham Kohestani Oct 10 '17 at 11:50
  • @ElhamKohestani Something about the parameters is not set up right and causing the result set to be empty. You'll have to track down what and make sure that the parameters are correct. Without being able to see your full set of code, I cannot see what's going on – DiskJunky Oct 10 '17 at 11:52