This is a migrated thread and some comments may be shown as answers.

[Solved] How to manipulate data in DB

3 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
FreshOne
Top achievements
Rank 1
FreshOne asked on 22 Feb 2010, 03:33 PM
hi all,
i just want to be sure that i'm working right with the grid..i manipulate data in the code behind, here is the ItemCommand sub

    Private Sub RadGrid2_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid2.ItemCommand  
        If e.CommandName = "Update" Then 
            If Page.IsValid Then 
                Dim item As GridEditableItem = CType(e.Item, GridEditableItem)  
                Dim objSection As New Sections(item.GetDataKeyValue("ID"))  
                objSection.SectionName = CType(item.FindControl("txtSectionName"), TextBox).Text  
                objSection.Update()  
                e.Item.Edit = False 
            End If 
        ElseIf e.CommandName = "PerformInsert" Then 
            If Page.IsValid Then 
                Dim item As GridEditableItem = CType(e.Item, GridEditableItem)  
                Dim objSection As New Sections()  
                objSection.SectionName = CType(item.FindControl("txtSectionName"), TextBox).Text  
                objSection.LangID = Session("Language")  
                objSection.SiteID = Session("Site")  
                objSection.Insert()  
                e.Canceled = True 
                e.Item.OwnerTableView.IsItemInserted = False 
                e.Item.OwnerTableView.Rebind()  
            End If 
        ElseIf e.CommandName = "Delete" Then 
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim objSection As New Sections(item.GetDataKeyValue("ID"))  
            objSection.Delete()  
        End If 
    End Sub 

It's functioning well, but i want to be sure that my approach is correct, and there is no easier and better approach to do it.
To be more specific:
1. to get user inputs, i have to use FindControl function right? 

2. to close the popup after success update i have to use
e.Item.Edit = False 

which don't work on insert so after success insert i use 
                e.Canceled = True    
                e.Item.OwnerTableView.IsItemInserted = False    
                e.Item.OwnerTableView.Rebind()    

3. on insert and update i need to use 
Dim item As GridEditableItem = CType(e.Item, GridEditableItem)   

but on delete i use
Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  


Wht do u think? am i getting it right?
thnx in advance

3 Answers, 1 is accepted

Sort by
0
robertw102
Top achievements
Rank 1
answered on 22 Feb 2010, 04:01 PM
I would suggest using the InsertCommand, UpdateCommand and DeleteCommand events, instead of handling the ItemCommand event, to handle the respective operations.
0
FreshOne
Top achievements
Rank 1
answered on 22 Feb 2010, 04:32 PM
hi robertw102 thnx for ur reply
why? wht's the difference? does it affect the performance?
0
robertw102
Top achievements
Rank 1
answered on 22 Feb 2010, 07:37 PM
It doesn't affect performance, although ItemCommand before the Insert/Update/Delete commands so two events get raised, it just makes more sense to handle the insert/update/delete operations using the correct event.
Tags
Grid
Asked by
FreshOne
Top achievements
Rank 1
Answers by
robertw102
Top achievements
Rank 1
FreshOne
Top achievements
Rank 1
Share this question
or