Out of Memory on localhost

Viewed 78

enter image description here

I am trying bind the data on index page some data gets display but after few seconds this error comes. I am using .net core 6 with oracle

I have tried all the option given in learn more and also tried to google the but I was not able to find solution

 public IActionResult Index()
    {
        var unitcode = HttpContext.Session.GetString("UnitCode");
        CommonViewModel.EmpMstslist = new List<EmpMst>();
        try
        {
            CommonViewModel.EmpMstslist = GetAllEmp();
        }catch(Exception ex)
        {

        }
        return View(CommonViewModel);
    }
    public IEnumerable<EmpMst> GetAllEmp()
    {
        var unitcode = HttpContext.Session.GetString("UnitCode");
        IEnumerable<EmpMst> empList;
        string query = "select EM_PNO,EM_NAME,EM_PASS,EM_STATUS,EM_HNAME,UNIT_CD from emp_mst where     UNIT_CD ='" + unitcode + "'";
        DataTable table = _context.GetSQLQuery(query.ToString());
        empList = (from DataRow dr in table.Rows
                   select new EmpMst()
                   {
                       EmPno = Convert.ToString(dr["EM_PNO"] != DBNull.Value ? Convert.ToString(dr["EM_PNO"]) : ""),
                       EmHname = Convert.ToString(dr["EM_HNAME"] != DBNull.Value ? Convert.ToString(dr["EM_HNAME"]) : ""),
                       EmName = Convert.ToString(dr["EM_NAME"] != DBNull.Value ? Convert.ToString(dr["EM_NAME"]) : ""),
                       EmStatus = Convert.ToString(dr["EM_STATUS"] != DBNull.Value ? Convert.ToString(dr["EM_STATUS"]) : ""),
                       UnitCd = Convert.ToString(dr["UNIT_CD"] != DBNull.Value ? Convert.ToString(dr["UNIT_CD"]) : ""),
                       EmPass = Convert.ToString(dr["EM_PASS"] != DBNull.Value ? Convert.ToString(dr["EM_PASS"]) : "")

                   }).OrderByDescending(x => x.EmName).ToList();

       

        return empList;
    }


<table class="table table-striped table-hover" id="HrmsGrid">
                            <thead>
                                <tr>
                                    <th> Unit Code </th>
                                    <th> Employee Personal No. </th>
                                    <th> Employee Name      </th>
                                    <th> Employee Pass      </th>
                                    <th> Employee HName     </th>
                                    <th> Employee Status    </th>
                                   
                                    <th class="table_cell">Action</th>
                                </tr>
                            </thead>


                            <tbody>
                             
                                        @foreach (var item in Model.EmpMstslist)
                                        {
                                            <tr>
                                                <td>@Html.DisplayFor(modelItem => item.UnitCd )</td>
                                                <td>@Html.DisplayFor(modelItem => item.EmPno)</td>                                     
                                                <td>@Html.DisplayFor(modelItem => item.EmName )</td>
                                                <td>@Html.DisplayFor(modelItem => item.EmPass)</td>
                                                <td>@Html.DisplayFor(modelItem => item.EmHname)</td>
                                                <td>@Html.DisplayFor(modelItem => item.EmStatus )</td>                                        
                                                <td>
                                                    <a asp-area="FASADM" asp-controller="FASM014" asp-action="Edit" asp-route-emp="@item.EmPno" asp-route-UnitCode="@item.UnitCd" class="addnewitem glpicon @Model.Edit" data-toggle="modal"><i class="far fa-edit" data-toggle="tooltip" title="" data-original-title="Edit" onclick="SHOW_TR_Function('traddpanelEdit','traddpanel')"></i></a>
                                                    <a class="delete glpicon @Model.Delete" style="cursor:pointer;" data-toggle="modal" onclick="divShow('/FASADM/FASM014/DeleteConfirmed?emp=@item.EmPno&&unitcd=@item.UnitCd');"><i class="far fa-trash-alt" data-toggle="tooltip" title="" data-original-title="Delete"></i></a>
                                                </td>
                                            </tr>
                                        }
                                   

                                  
                            </tbody>

                        </table>

the amount of data returned by query is 22358 rows. I have also tried adding this to <requestLimits maxAllowedContentLength="2147483648" /> web.config file.

1 Answers

These are many rows. Try to load only a part of it and add more, if they are needed.

You could do this in the SQL-Statement:

select EM_PNO,EM_NAME,EM_PASS,EM_STATUS,EM_HNAME,UNIT_CD 
    from emp_mst where     UNIT_CD ='unitcode' 
OFFSET     0 ROWS   -- skip 0 rows
FETCH NEXT 100 ROWS ONLY; -- take 10 rows

Same is possible with Entity Framework

        query.Skip(0).Take(100)
               .OrderByDescending(x => x.EmName).ToList();

But as mentioned in the comments: Remove the Convert.ToString(...) and address the SQL injection problem.


As mentioned you have to show all the data at once. That's not recommendable, but well...

The OutOfMemory exception happens in Chrome and not in the application. You could try some things, to get a grasp what happens:

  1. Try Chrome in incognito mode
  2. Try Firefox/Edge/etc.
  3. Reset Chrome, delete user profile

Two other things you could try:

  1. In the view don't use a table, but insert just the content one column of every row. Something like this:
    <div>
        @foreach(var row in CommonViewModel.EmpMstslist) 
        {
            <div>@row.EmPno</div>
        }
    </div>
  1. Write the result in a simple txt file and check the size.
Related