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

Radgrid Edit Form Update through TableAdapter

1 Answer 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 09 May 2015, 02:37 AM

I am having an issue with following the demo located at (http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/edit-form-types/defaultcs.aspx)

I have been able to get the radgrid to fill with my datasource, see the edit column and popsup the edit form as I have formated it. But when I make the "Edit" and choose the "Update" nothing happens. The values seem to be getting saved, but there not going to the database. The AcceptChanges() command is where I believe it is not saving to the database.  

Below is my code behind. Any help would be appreciated. Thank you in advance.

 

Partial Class Member

Public Function GetDataTable(ByVal query As String) As DataTable 

        Dim ConnString As String = ConfigurationManager.ConnectionStrings("DatabaseName").ConnectionString

        Dim conn As SqlConnection = New SqlConnection(ConnString)

        Dim adapter As SqlDataAdapter = New SqlDataAdapter

        adapter.SelectCommand = New SqlCommand(query, conn)

        Dim table1 As New DataTable

        conn.Open()

        Try

            adapter.Fill(table1)

        Finally

            conn.Close()

        End Try

        Return table1

    End Function 

    Public ReadOnly Property Members() As DataTable

        Get

            Dim obj As Object = Me.Session("Members")

            If (Not obj Is Nothing) Then

                Return CType(obj, DataTable)

            End If

    Dim dtblMember As New Main.tblMemberDataTable       

dtblMember = memberAdapter.GetAllMemberInfo()

            Me.Session("Members") = dtblMember

            Return dtblMember 

        End Get

    End Property

    Protected Sub rgMember_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles rgMember.NeedDataSource 

        Me.rgMember.DataSource = Me.Members

        Me.Members.PrimaryKey = New DataColumn() {Me.Members.Columns("MemberID")}

 End Sub

     Protected Sub rgMember_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles rgMember.PreRender

        If Not IsPostBack And Me.rgMember.MasterTableView.Items.Count > 1 Then

            'This sets the specified item to default to edit mode on load when set to True.

            Me.rgMember.MasterTableView.Items(1).Edit = False           

            Me.rgMember.MasterTableView.Rebind()

        End If

    End Sub

    Protected Sub rgMember_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgMember.UpdateCommand

        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)

        Dim MyUserControl As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)

'Locate the changed row in the DataSource
        Dim changedRows As DataRow() = Me.Members.Select("MemberID = " & editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("MemberID"))

        If (Not changedRows.Length = 1) Then

            e.Canceled = True

            Return

        End If 

        'Update new values

        Dim newValues As Hashtable = New Hashtable 

        newValues("Designation") = CType(MyUserControl.FindControl("txtDesignation"), TextBox).Text

        newValues("AppointedDate") = CType(MyUserControl.FindControl("rdpAppointedDate"), RadDatePicker).SelectedDate.ToString()

        newValues("EffectiveDate") = CType(MyUserControl.FindControl("rdpEffectiveDate"), RadDatePicker).SelectedDate.ToString()

        newValues("TermExpireDate") = CType(MyUserControl.FindControl("rdpTermExpireDate"), RadDatePicker).SelectedDate.ToString()

        newValues("ChairID") = CType(MyUserControl.FindControl("ddlChair"), RadDropDownList).SelectedItem.Value

        newValues("SeatTypeID") = CType(MyUserControl.FindControl("ddlSeatType"), RadDropDownList).SelectedItem.Value

        newValues("Replacing") = CType(MyUserControl.FindControl("txtReplacing"), TextBox).Text 

        changedRows(0).BeginEdit()

        Try

            Dim entry As DictionaryEntry

            For Each entry In newValues

                changedRows(0)(CType(entry.Key, String)) = entry.Value

            Next

            changedRows(0).EndEdit()

            Me.Members.AcceptChanges()

        Catch ex As Exception

            changedRows(0).CancelEdit() 

            Dim lblError As Label = New Label()

            lblError.Text = "Unable to update Board Member. Reason: " + ex.Message

            lblError.ForeColor = System.Drawing.Color.Red

            rgMember.Controls.Add(lblError) 

            e.Canceled = True

        End Try

    End Sub

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 13 May 2015, 08:28 AM
Hi Chris,

As we have discussed in the support ticket that you have opened (Ticket ID: 933629), the AcceptChanges method is used to update the DataTable object stored in the Session and the underlying database will not be updated. 

Detailed information on the AcceptChanges method could be found in the following MSDN article:

Best Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or