How to update or delete in Grid using edit option button and objectdatasource for vb.net

Hello,

I have an existing project which is using asp:ListView's UpdateMethod, InsertMethod and so on.
I want to switch to ej:grid using objectdatasouce.  We are using Entity Framework/Linq to SQL.
I also want to use command column button( edit, save, and delete ) .  I plan to use UpdateMethod and InsertMethod which is already in code behind.

Code behind is made by VB.Net.  Is there any sample solution like this?  
If you have, It will help me a lot.

Thank you in advance.

Yukiko




9 Replies

VN Vignesh Natarajan Syncfusion Team July 16, 2018 10:32 AM UTC

Hi Yukiko, 

Thanks for using Syncfusion products.  

According to your requirement, you need to perform CRUD operation in Grid using objectdataSource. We have prepared a sample which can be downloaded from below link 


Refer the below code snippet 

<ej:Grid ID="Grid1" runat="server" AllowPaging="True"  DataSourceID="test34">  
        <EditSettings AllowEditing="true" AllowAdding="true" AllowEditOnDblClick="false" AllowDeleting="true" /> 
        <ToolbarSettings ToolbarItems="add,edit,delete,update,cancel" ShowToolbar="true"></ToolbarSettings> 
        <Columns> 
            <ej:Column Field="CustomerID" IsPrimaryKey="true"/> 
            <ej:Column Field="CompanyName" />             
            <ej:Column Field="ContactName" /> 
             <ej:Column HeaderText="Manage Column" IsUnbound="True" Width="130"> 
                    <Command> 
                        <ej:Commands Type="edit"> 
                            <ButtonOptions Text="Edit"></ButtonOptions> 
                        </ej:Commands> 
                        <ej:Commands Type="delete"> 
                            <ButtonOptions Text="Delete"></ButtonOptions> 
                        </ej:Commands> 
                        <ej:Commands Type="save"> 
                            <ButtonOptions Text="Save"></ButtonOptions> 
                        </ej:Commands> 
                        <ej:Commands Type="cancel"> 
                            <ButtonOptions Text="Cancel"></ButtonOptions> 
                        </ej:Commands> 
                    </Command> 
                </ej:Column> 
        </Columns> 
    </ej:Grid> 
<asp:ObjectDataSource runat="server" ID="test34" SelectMethod="BindDataSource" DeleteMethod="DeleteRecord"     
             UpdateMethod="EditRecord"  InsertMethod="AddRecord" OnUpdating="test34_Updating"   
             TypeName="Sample.GridData"   
    DataObjectTypeName="Sample.C30000Records" >  
    </asp:ObjectDataSource> 
 
[ASPX.cs.vb] 
 
<DataObjectMethod(DataObjectMethodType.Update)> 
    Public Shared Function EditRecord(ord As C30000Records) As C30000Records 
        'Do your operations here 
        Return ord 
    End Function 
    <DataObjectMethod(DataObjectMethodType.Insert)> 
    Public Shared Function AddRecord(ord As C30000Records) As C30000Records 
        'Do your operations here 
        Return ord 
    End Function 
    <DataObjectMethod(DataObjectMethodType.Delete)> 
    Public Shared Function DeleteRecord(ord As C30000Records) As C30000Records 
        'Do your operations here 
        Return ord 
    End Function 

Please get back to us if you need further assistance. 

Regards, 
Vignesh Natarajan  




YI Yukiko Imazu July 16, 2018 03:25 PM UTC

Hi Vignesh

Thank you so much for the vb.net sample.  It helped a lot.

I created project with .Net framework 4.5 as followed your sample.
SelectMethod works well.  When it comes to update, it doesn't work.
I set breakpoint to see it actually comes into either EditRecord method or test34_Updating method.
When I run the project and click save button in GridView, it didn't stop anywhere.

I attach my sample project.

I'm looking for a this kind of forums, but I couldn't find it.
I would appreciate it If you give me a sample which actually update datasource. 
I want to use TryUpdateModel for update.


Best Regards,
Yukiko




Attachment: GridTestDB_7f36990.7z


VN Vignesh Natarajan Syncfusion Team July 17, 2018 05:19 PM UTC

Hi Yukiko, 

Sorry for the inconvenience caused. 

For your convenience we have modified the sample to perform editing (CRUD operation) using Object DataSource.  

Kindly download the modified sample from below link 


Please get back to us if you have any queries. 

Regards, 
Vignesh Natarajan.  



YI Yukiko Imazu July 17, 2018 09:05 PM UTC

Hi Vignesh,

Thank you for your modified sample.
I created my project as followed your sample, but it still doesn't stop at the breakpoint in a code behind function 
when I click save or delete button in the grid.  It seems like click event not working.

I attach my project. Could you please tell me what is wrong my code?
(I haven't implemented anything inside function yet.)

Best Regards,
Yukiko

Attachment: GridEditTest_ac882416.7z


VN Vignesh Natarajan Syncfusion Team July 19, 2018 12:24 PM UTC

Hi Yukiko, 

Thanks for your update.  

We have analyzed the provided sample and we suspect that there is some issue with the dataSource and its type in the member class. Because we have modified the sample to update the record by creating a local dataSource and providing full complete code inside each method.  
We are not able to reproduce the reported issue (UpdateMethod is not working while using Object DataSource) at our end. We have attached the modified sample below 


In above sample we have modified the class name as members and tried updating it. Refer the below screenshot for the output 

 
  

Reported issue (UpdateMethod is not working while using Object DataSource) may occur when dataSource type differs. So kindly ensure your dataSource and if you still face the error please get back to us. 

Regards, 
Vignesh Natarajan      



YI Yukiko Imazu July 20, 2018 04:41 PM UTC

Hi Vignesh,

Thank you for your modified sample.

I could debug until EditRecord method.  Actually, My requirement is using TryUpdateModel inside EditRecord function(Update function).
When I run the project, it gives me NullReferenceException.
Is it possible for us to use TryUpdateModel function for ej:grid?

I attach my sample.

Thank you,
Yukiko

Attachment: GridUpdate_ec089f8.7z


VN Vignesh Natarajan Syncfusion Team July 23, 2018 12:41 PM UTC

Hi Yukiko, 

Thanks for the update.  

According to your query, you are facing issue while calling the TryUpdateModel method of System.Web. We have analyzed your sample and we are able to reproduce the reported issue at our end. We suspect that it is not possible to call TryUpdateModel directly. So we suggest you to override the TryUpdateModel using Shared function and perform your actions in the shared function. 

Refer the below code snippet 

  Public Shared Function TryUpdateModel(ord As member) 
        Return ord 
    End Function 
 
 
    <DataObjectMethod(DataObjectMethodType.Update)> 
    Public Function EditRecord(ord As member) As member 
        'Do your operations here 
        Dim sc As New Database1Entities() 
        Dim product = sc.members.Find(ord.memberId) 
        TryUpdateModel(ord) 
        Return ord 
 
    End Function 
      
If above solution does not resolve your query, kindly share the following details  
 
  1. Why do want to call System.web’s TryUpdateModel?. Because in most of case it in not suggested while updating the data. 
  2. Are you facing issue while updating the dataSource directly?. If yes share the screenshot of script error in console window. 
 
Regards, 
Vignesh Natarajan 



YI Yukiko Imazu July 23, 2018 02:39 PM UTC

Hi Vignesh,

     I tried to override the TryUpdateModel using Shared function, but it inherited UserControl.  I cannot use override.

  1. Why do want to call System.web’s TryUpdateModel?. Because in most of case it in not suggested while updating the data.  
          Because we want to use existing code.  We used TryUpdateModel when we update, delete, and insert.  Why is it not suggested while updating the data?


     I also need to use Session data in those method, but it seems like I cannot get session data.

     Is there any good way to solve this problem for grid?

Regards,
Yukiko



VN Vignesh Natarajan Syncfusion Team July 27, 2018 11:46 AM UTC

Hi Yukiko, 
 
 
Sorry for the inconvenience caused.  
 
A support incident has been created under your account to track the status of this requirement. Please log on to our support website to check for further updates.  
 
 
 
Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon