I cannot get my SQL data into the grid

I can't seem to get the grid to work with my database. everything works fine until I take out the prebuilt data, and try to get it to pull data from my models. 

Just shows No results found.

I'm under the impression that I don't understand the Datamanager documentation. I've tried what feels like every possible scenario in the docs. any help would be appreciated.


6 Replies

BS Balaji Sekar Syncfusion Team November 24, 2021 07:16 AM UTC

Hi Steven, 

Greetings from the Syncfusion support 

Based on your query we have already discussed the databing of Sql database data in Syncfusion DataGrid component . please refer the below link for more information. 


You can achieve Sql data binding in the DataGrid based on above KB. 

Regards, 
Balaji Sekar 



ST Steven November 24, 2021 08:32 PM UTC

When I use this method I get the following error:

  • KeyNotFoundException: The given key 'CurrentClass' was not present in the dictionary.

    • System.Collections.Generic.Dictionary.get_Item(TKey key)

    • Syncfusion.EJ2.EJTagHelper.GetChildContent(TagHelperContext context, TagHelperOutput output)

    • Syncfusion.EJ2.EJTagHelper.Process(TagHelperContext context, TagHelperOutput output)

    • Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)

    • Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)

    • AspNetCore.Views_LocationModels_Index.ExecuteAsync()

    • Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)

    • Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)

    • Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)

    • Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable statusCode)

    • Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable statusCode)

    • Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable statusCode)

    • Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)

    • Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext(ref State next, ref Scope scope, ref object state, ref bool isCompleted)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()

    • Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)

    • Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)

    • Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)

    • Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)

    • Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)

    • Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

If I remove "

<e-data-manager url="LocationModels/UrlDatasource" insertUrl="/LocationModels/Insert" updateUrl="/LocationModels/Update" removeUrl="/LocationModels/Remove" adaptor="UrlAdaptor"></e-data-manager>

" from the View file index.cshtml
It will load, but without data, obviously.



ST Steven November 24, 2021 09:50 PM UTC

I moved the <e-data-manager> tags within the <ejs-grid> tags and got rid of the error, however, the page now loads, but without a table/grid/data anthing.



BS Balaji Sekar Syncfusion Team November 25, 2021 08:43 AM UTC

Hi Steven, 

Thanks for your update. 

We have prepared a sample for CRUD Operation with URL adaptor to modify the SQL data source. In the below  sample we have used Northwind DB to display the data in Grid and perform CRUD actions. Please refer below code example, sample for more information.  

<ejs-grid id="Grid" height="272" allowPaging="true" allowSorting="true" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update","Cancel" })"> 
  <e-data-manager url="/DataGrid/UrlDataSource" adaptor="UrlAdaptor" insertUrl="/DataGrid/Insert" updateUrl="/DataGrid/Update" removeUrl="/DataGrid/Delete"></e-data-manager> 
  <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings> 
    <e-grid-columns> 
        <e-grid-column field="OrderID"  headerText="OrderID" isIdentity="true" defaultValue=0 isPrimaryKey="true"></e-grid-column> 
       <e-grid-column field="CustomerID"  headerText="CustomerID"></e-grid-column> 
    </e-grid-columns> 
</ejs-grid> 
 public class DataGridController : Controller 
  { 
      private OrderDbContext _context; 
 
    public DataGridController(OrderDbContext Context) 
    { 
            this._context=Context; 
    } 
        public ActionResult Index() 
        { 
          return View(); 
        } 
        public ActionResult UrlDatasource([FromBody]DataManagerRequest dm) 
        { 
       // here we are retrieving the data from DB to display the records in grid 
            IEnumerable DataSource = _context.Orders.ToList(); 
 
            DataOperations operation = new DataOperations(); 
            if (dm.Sorted != null && dm.Sorted.Count > 0//Sorting 
            { 
                DataSource = operation.PerformSorting(DataSource, dm.Sorted); 
            } 
            int count = DataSource.Cast<Orders>().Count(); 
            if (dm.Skip != 0)//Paging 
            { 
                DataSource = operation.PerformSkip(DataSource, dm.Skip);          
            } 
            if (dm.Take != 0) 
            { 
                DataSource = operation.PerformTake(DataSource, dm.Take); 
            } 
            
            return Json(new { result = DataSource, count = count }); 
        } 
        public ActionResult Insert([FromBody]CRUDModel<Orders>  value) 
        { 
            //here you can add data to your db 
            _context.Orders.Add(value.Value); 
            _context.SaveChanges(); 
            return Json(value); 
        } 
        public ActionResult Update([FromBody]CRUDModel<Orders>  value) 
        { 
                        //here you can update data in your db 
      var ord = value; 
 
           Orders val = _context.Orders.Where(or => or.OrderID == ord.Value.OrderID).FirstOrDefault(); 
            val.OrderID=ord.Value.OrderID; 
            val.CustomerID=ord.Value.CustomerID; 
            _context.SaveChanges(); 
            return Json(value); 
        } 
        public ActionResult Delete([FromBody]CRUDModel<Orders> value) 
        { 
 
              //here you can perform delete action your db 
            Orders order = _context.Orders.Where(c => c.OrderID == (int)value.Key).FirstOrDefault(); 
            _context.Orders.Remove(order); 
            _context.SaveChanges(); 
            return Json(order); 
        } 
   } 
{ 
    public class OrderDbContextDbContext 
    { 
        public virtual DbSet<OrdersOrders { getset; } 
 
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        { 
            if (!optionsBuilder.IsConfigured) 
            { 
           // here we have accessed the NorthWind DB 
                string directory = Directory.GetCurrentDirectory(); 
                string dir = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=" + directory + \\AppData\\NORTHWND.MDF;Integrated Security=True; 
                optionsBuilder.UseSqlServer(dir); 
            } 
        } 
    } 
} 
 

If still facing same problem, please replicate the issue above in sample and share to us that will help to validate further. 

Regards, 
Balaji Sekar 



ST Steven November 25, 2021 11:29 PM UTC

Thank  you I believe this has gotten it working.



RR Rajapandi Ravi Syncfusion Team November 26, 2021 01:34 PM UTC

Hi Steven, 

Thanks for the update 

We are happy to hear that our provided solution was working fine at your end. 

Please get back us if you need further assistance. 

Rajapandi R 



Loader.
Up arrow icon