Programatically bind dropdownlist using OpenAccess

6 posts, 0 answers
  1. Lisa
    Lisa avatar
    16 posts
    Member since:
    Nov 2013

    Posted 08 Apr 2014 Link to this post

    I have a simple  textbox and a simple dropdownlist like this:
    <asp:TextBox ID="txtState" runat="server" Width="200px" AutoPostBack="true"    ></asp:TextBox>
     <asp:DropDownList ID="cboCities" runat="server"  Width="200px" AutoPostBack="true" >  </asp:DropDownList> 

    When the user enters a state in the textbox, I want to fill the dropdownlist with appropriate cities. I am using an Open Access Data Model.I have tried several different ways to bind the dropdownlist in the page’s load complete event when it detects that txtState.text <> “” and cboCities.items.count = 0

    'Attempt 1
     'No errors but no values filled either. If it did work I would want to replace the where clause with c.State = txtState.text
    Using model As New MyDataModel.EntitiesModel
           Dim Stations As List(Of City) = (From c In model.Cities Where c.State = "MA" _ And c.IsActive = 1).ToList
           If Not Stations Is Nothing Then
                cboCities.DataSource = Stations
                cboCities.DataBind()
           End If
    End Using

    'Attempt 2 
      ''this one works but doesn't have the ability to filter by state. DBContext is defined on the DataAccesPage which this page inherits from
      cboCities.DataSource = DbContext.Cities.ToList()
      cboCities.DataTextField = "CityID"
      cboCities.DataBind()

       'Attempt 3
    'THIS ONE WORKS TO FILL THE CBO BUT ANY CODE AFTER THIS THROWS ERRORS I really wanted this one to work because it calls a function in my '     'BLL (FillCitiesList) which seems the most “Proper” way to go about it

    Dim listStations As IQueryable(Of CanFillStation)
    listStations = FillCitiesList ("MA”)
    cboCities.DataSource = listStations
    cboCities.DataBind() 

    Public Function FillCitiesList (ByVal stState As String) As IQueryable(Of City)
            Using model As New MyDataModel.EntitiesModel
                Dim query = From c In model.Cities Where c.State = strState
                   Select c
                Return query
            End Using
        End Function 

    This is the error message that is thrown after attempt 3 code executes:
    System.ObjectDisposedException was unhandled by user code  Message=The ObjectScope has already been disposed and it's managed persistent objects can no longer be accessed. The ObjectScope should be disposed at the end of the life cycle of your business logic instance. This can also be done in the Dispose of your ASP page or MVC controller.Object name: 'OpenAccessRuntime.EnlistableObjectScope'.  ObjectName=OpenAccessRuntime.EnlistableObjectScope  Source=Telerik.OpenAccess.Runtime  StackTrace:       at OpenAccessRuntime.ObjectScope.CheckNotDisposed()       at OpenAccessRuntime.ObjectScope.Telerik.OpenAccess.SPI.IExtendedObjectScope.get_ShouldLogExpressions()       at Telerik.OpenAccess.Query.ExtentQueryImpl`1.Telerik.OpenAccess.Query.ExecutionSettings.get_ShouldLogExpressions()       at Telerik.OpenAccess.Query.ExpressionCutter.CutAndExecute(Expression expression, ExecutionSettings settings, Expression whole)       at Telerik.OpenAccess.Query.Piece`1.ExecuteMultiple()       at Telerik.OpenAccess.Query.Piece`1.System.Collections.IEnumerable.GetEnumerator()       at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource)       at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)       at System.Web.UI.WebControls.ListControl.PerformSelect()       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()       at ELSATracking.EditRelations.Page_LoadComplete(Object sender, EventArgs e) in C:\_DEVELOPMENT\ELSATracking\ELSATracking\UserPages\Shop\EditRelations.aspx.vb:line 132       at System.Web.UI.Page.OnLoadComplete(EventArgs e)       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)  InnerException:


    Obviously, I don't know what I'm doing! Can anyone help me fill the dropdownlist programatically after the user enters a state, using OpenAccess, and preferably using a business layer function?

    Thanks
  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 11 Apr 2014 Link to this post

    Hello,

    I would suggest taking a look at this forum discussion about pretty much same problem and try the provided approach.


    Regards,
    Boyan Dimitrov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Lisa
    Lisa avatar
    16 posts
    Member since:
    Nov 2013

    Posted 13 Apr 2014 in reply to Boyan Dimitrov Link to this post

    Sorry, that link didn't really help me.
    I am simply looking for the code-behind that can be used to fill a dropdownlist using an OpenAccess datasource and a where clause.

    BTW, there is a cut and paste error in my posted code:
    Dim listStations As IQueryable(Of CanFillStation) should be Dim listStations As IQueryable(Of Cities)
    but that isn't the problem.

    Lisa
  5. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 14 Apr 2014 Link to this post

    Hello Lisa,

    It would very helpful if you try to populate the standard asp:DropDownList control and let us know how that worked.


    Regards,
    Boyan Dimitrov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Lisa
    Lisa avatar
    16 posts
    Member since:
    Nov 2013

    Posted 15 Apr 2014 in reply to Boyan Dimitrov Link to this post

    It IS a regular asp drop down list. I don't know the proper syntax for filling it dynamically with data using my Open Access data model. I could just use a SQL data source but I'd really like to use a function that retrieves the data from the model with a where clause and then bind that function result to my dropdown (something like the third attempt I posted.)
  7. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 18 Apr 2014 Link to this post

    Hi Lisa,

    The attached sample solution demonstrates an implementation of the scenario on you side. It uses Telerik Data Access in the data access layer and Telerik UI for ASP.NET AJAX controls in the user interface. As an approach for the management of the Data Access context, the sample utilizes the pattern described in this article. The actual filtering of the data is performed in the method that handles the OnClick event of the button on the page. A key moment in the implementation is that the result is enumerated (the ToList() call) before it is passed as a datasource for the dropdown box.

    The sample is based on the NorthwindOA sample database that is distributed with Telerik Data Access Samples Kit, and can be started with the help of the following steps:
    1. Unzip the project.
    2. Edit the connection string in the Web.config file of the web application, to provide the name of the server on your side.
    3. On the server, create the NorthwindOA sample database (you can take it from the install folder of Samples Kit once it is installed on the machine).
    4. Open the solution in Visual Studio and upgrade the references as described here.
    5. Run the application for a test.



    Regards,
    Doroteya
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017