We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

I can viewing & Editing but can not insert new record


I have made scaffolding using (syncfusion) Extinsion, for one model by selecting remote data and here is the scaffolding controler:

 ================================

 public class BranchController : Controller

  {

    private PSDbContext _context;


public BranchController(PSDbContext Context)

{

            this._context=Context;

}

        public ActionResult Index()

        {

        return View();

        }

        public ActionResult UrlDatasource([FromBody]DataManagerRequest dm)

        {


            IEnumerable DataSource = _context.Branch.ToList();


            DataOperations operation = new DataOperations();

            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting

            {

               DataSource = operation.PerformSorting(DataSource, dm.Sorted);

            }

       if (dm.Search != null && dm.Search.Count > 0)

           {

               DataSource = operation.PerformSearching(DataSource, dm.Search); //Search

           }

if (dm.Where != null && dm.Where.Count > 0) //Filtering

            {

    DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);

            }

            int count = DataSource.Cast<Branch>().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<Branch> value)

        {

            //do stuff

_context.Branch.Add(value.Value);

_context.SaveChanges();

            return Json(value);


        }

        public ActionResult Update([FromBody]CRUDModel<Branch> value)

        {

            //do stuff

var ord = value;


           Branch val = _context.Branch.Where(or => or.BranchId == ord.Value.BranchId).FirstOrDefault();

            val.BranchId=ord.Value.BranchId;

            val.Name=ord.Value.Name;

            val.Description=ord.Value.Description;

            _context.SaveChanges();

            return Json(value);

        }

        public ActionResult Delete([FromBody]CRUDModel<Branch> value)

        {

            //do stuff

Branch order = _context.Branch.Where(c => c.BranchId == (int)value.Key).FirstOrDefault();

            _context.Branch.Remove(order);

            _context.SaveChanges();

            return Json(order);

        }

   }

==========================================



 and Also this is the view file generated after scaffolding:

=========================================

<ejs-grid id="Grid" height="272" allowPaging="true" allowSorting="true" toolbar="@(new List<string>() { "Search", "Add", "Edit", "Delete","Update","Cancel" , "ExcelExport" , "CsvExport" ,"PdfExport"})" allowFiltering="true" allowSelection="true" allowExcelExport="true" allowPdfExport="true" toolbarClick="toolbarClick">

  <e-data-manager url="/Branch/UrlDataSource" adaptor="UrlAdaptor" insertUrl="/Branch/Insert" updateUrl="/Branch/Update" removeUrl="/Branch/Delete"></e-data-manager>

  <e-grid-filterSettings type="Excel"></e-grid-filterSettings>

<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog"></e-grid-editSettings>

<e-grid-selectionsettings type="Single"></e-grid-selectionsettings>

    <e-grid-columns>

        <e-grid-column field="BranchId" headerText="BranchId" isPrimaryKey="true" isIdentity="true"></e-grid-column>

       <e-grid-column field="Name" headerText="Name"></e-grid-column>

       <e-grid-column field="Description" headerText="Description"></e-grid-column>

    </e-grid-columns>

</ejs-grid>



<script>

function toolbarClick(args)

{

  if (args.item.id === this.element.id+'_pdfexport')

  {

    this.pdfExport();

  }

   if (args.item.id === this.element.id+'_csvexport')

   {

    this.csvExport();

   }

  if (args.item.id === this.element.id + '_excelexport')

  {

    this.excelExport();

  }

}

</script>


==========================================


Now the connecting to SQL server is working good and i can view data but when I adding new records I got this error

{reference not set to an instance of an object }


in this line:

_context.Branch.Add(value.Value);




Any help please


4 Replies 1 reply marked as answer

RS Rajapandiyan Settu Syncfusion Team October 14, 2022 08:07 AM

Hi Majid,


Thanks for contacting Syncfusion support.

We tried to reproduce the reported problem but it was unsuccessful from our side. Find the below sample for your reference.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/core_grid_url_crud_insert1394669976.zip

Please ensure that are you able to get the added value properly in the server side.


Screenshot #1: Request to the server on inserting a record


Screenshot #2: Getting the inserted record details in the server method


screenshot #3: Response from the server


Still, if you face the same issue, kindly share the below details to validate further.


  1. Share the screenshot of Network Tab Request and Response.
  2. Are you able to get the added value properly at the server?
  3. Share the video demo of the reported problem.
  4. If possible, share the issue reproducible sample which will be very helpful for us.


Regards,

Rajapandiyan S
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



MF Majid Farhan October 14, 2022 12:13 PM

Thank you for your answer.

This is the breakpoint :

test1.png


I found this error in Network Tab 

test.png



MF Majid Farhan October 15, 2022 05:05 PM

This is the solution I found it in sycnfusion forum:

just to add (nullable) in Key property :)

public Nullable<int> BranchId { get; set; } 



Marked as answer

RS Rajapandiyan Settu Syncfusion Team October 16, 2022 10:50 PM

Hi Majid,


We are happy to hear that you have resolved the reported problem by setting the number field as a nullable value type in the class definition.


Please get back to us if you need further assistance.


Regards,

Rajapandiyan S


Loader.
Live Chat Icon For mobile
Up arrow icon