Listbox will not initialize with data from remote database

I have a page with the following:
@page "/Reports/WestInterlake"
<div class="container">
    <div id="listbox-control">
        <h4>Select Lagoon:</h4>
        <SfListBox @ref="lagoonListBox" DataSource="lagoonList" TValue="string[]" TItem="Lagoon" @bind-Value="@lagoonNames">
            <ListBoxFieldSettings Text="Name" Value="Id"></ListBoxFieldSettings>
        </SfListBox>
    </div>
...
</div>
@code {
    SfListBox<string[], Lagoon> lagoonListBox;
    List<Lagoon> lagoonList;
    public string[] lagoonNames;

    protected override async Task OnInitializedAsync()
    {
        await Task.Run(() =>
        {
            lagoonList = context.Lagoons.Where(n => n.IsActive == true).ToList<Lagoon>();
            lagoonList.Sort();
        });
    }
}

This page has an EF Core datacontext injected, and the connection string is in the appsettings.Development.json file:
  "Data": {
    "DefaultConnection": {
//            "ConnectionString": "Data Source=XXX\\SQLEXPRESS;Initial Catalog=xxx;Integrated Security=False;User ID=xxxx;Password=xxxx;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
              "ConnectionString": "Data Source=XX.XXX.149.20,XXX;Initial Catalog=xxx;Integrated Security=False;User ID=xxxx;Password=xxxx;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
    }
  }

The top commented-out connection string is to my local SQLExpress server.  When the top connection string is active, this page works, even though it takes a long time for the data to load.  When the bottom string is active, lagoonList is populated (5 records), but the ListBox always shows "No Records Found", no matter how long I wait.  The sql databases at either location are exactly the same, and I have a bunch of other pages that use the same data, and they all work fine no matter which connection string is active, the only difference being that they don't use ListBox.  I have tried setting the Connect Timeout to 45, but that made no difference.

Thanks for any help you can give me.


2 Replies 1 reply marked as answer

MI Mike April 18, 2021 05:38 PM UTC

Turns out the problem was noob programming. I was using lazy loading, so there wasn't actually 5 records, the complex properties were loading in tens of thousands of records. Switched to eager loading, and now it works great :) Figured I'd leave this here, though, so that if any other beginners like me where running into performance issues, this might help.


AS Aravinthan Seetharaman Syncfusion Team April 23, 2021 12:44 PM UTC

Hi Mike, 
 
Thanks for contacting Syncfusion Support. 
 
We have checked your query. We suspect that you are trying to fetch data from database. So, based on this scenario we have prepared code snippet and sample here. 
 
 
@using Syncfusion.Blazor.Data; 
@using Syncfusion.Blazor; 
@using Syncfusion.Blazor.DropDowns; 
 
 
<SfListBox TValue="string[]" TItem="Order" Query="RemoteDataQuery"> 
    <SfDataManager Adaptor="Adaptors.CustomAdaptor"> 
        <CustomAdaptorComponent></CustomAdaptorComponent> 
    </SfDataManager> 
    <ListBoxFieldSettings Text=@nameof(Order.CustomerID)></ListBoxFieldSettings> 
</SfListBox> 
@code{  
 
    public static List<Order> Orders { get; set; } 
    public Query RemoteDataQuery = new Query().Select(new List<string> { "CustomerID" }).Take(6).RequiresCount(); 
    public class Order 
    { 
        public int? OrderID { get; set; } 
        public string CustomerID { get; set; } 
    }  
} 
 
 
 
CustomAdaptorComponent.razor 
 
@inject IHostingEnvironment _env 
 
@inherits DataAdaptor<Order> 
 
<CascadingValue Value="@this"> 
    @ChildContent 
</CascadingValue> 
 
@code { 
    [Parameter] 
    [JsonIgnore] 
    public RenderFragment ChildContent { get; set; } 
    public static DataSet CreateCommand(string queryString, string connectionString) 
    { 
        using (SqlConnection connection = new SqlConnection( 
                   connectionString)) 
        { 
 
            SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection); 
            DataSet dt = new DataSet(); 
            try 
            { 
                connection.Open(); 
                adapter.Fill(dt);// using sqlDataAdapter we process the query string and fill the data into dataset 
            } 
            catch (SqlException se) 
            { 
                Console.WriteLine(se.ToString()); 
            } 
            finally 
            { 
                connection.Close(); 
            } 
            return dt; 
        } 
    } 
    // Performs data Read operation 
    public override object Read(DataManagerRequest dm, string key = null) 
    { 
        string appdata = _env.ContentRootPath; 
        string path = Path.Combine(appdata, "App_Data\\NORTHWND.MDF"); 
        string str = $"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename='{path}';Integrated Security=True;Connect Timeout=30"; 
        // based on the skip and take count from DataManagerRequest here we formed SQL query string 
        string qs = "SELECT CustomerID FROM dbo.Orders ORDER BY OrderID OFFSET " + dm.Skip + " ROWS FETCH NEXT " + dm.Take + " ROWS ONLY;"; 
        DataSet data = CreateCommand(qs, str); 
        Orders = data.Tables[0].AsEnumerable().Select(r => new Order 
        { 
 
            CustomerID = r.Field<string>("CustomerID") 
        }).ToList();  // here we convert dataset into list 
        IEnumerable<Order> DataSource = Orders; 
        SqlConnection conn = new SqlConnection(str); 
        conn.Open(); 
        SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM dbo.Orders", conn); 
        Int32 count = (Int32)comm.ExecuteScalar(); 
        return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource; 
    } 
} 
 

If we misunderstood your query, please share below details 

·        Please share more details regarding your query. 
·        If possible, try to reproduce the reported issue in provided sample or share the issue reproducible sample. 
·        Please share us the video demonstration of this issue. 
·        Please share us the Syncfusion Package Version. 
 
 
Please provide the above requested information, based on that we will check and provide you a better solution quickly. 

Regards, 
Aravinthan S

Marked as answer
Loader.
Up arrow icon