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

RadGrid updating but not in my database table help plss

2 Answers 67 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
robin asked on 25 Jul 2010, 11:48 AM
This code is loosely based on several of the demos. My main problem here is that when i run my program and happen to see my gridview the datas are not the same as with my datas in my table in my database.

And when i tried to hit the edit button.. and make some few changes.. it updates my gridview but not the same as my datas in my table in my database..

hope someone could lend a helping hand..

here's my code.. by the way i'm just a newbie in ..please be gentle.. thanks


<%@ Page Language="VB" MasterPageFile="~/Admin/Admin.master" AutoEventWireup="false" CodeFile="TeacherRole.aspx.vb" Inherits="Admin_TeacherRole" title="Untitled Page" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
       <!-- content start -->
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function RowDblClick(sender, eventArgs) {
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" 
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
       <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True"
            AllowPaging="True" AllowSorting="True" OnUpdateCommand = "RadGrid1_UpdateCommand"
            AutoGenerateEditColumn="True"  GridLines="None"
        AllowAutomaticUpdates="True" ShowGroupPanel="True" GroupPanel-ID = "RadAjaxLoadingPanel1">
             <MasterTableView EditMode="PopUp" AutoGenerateColumns="False"  DataKeyNames = "user_id"
                     <telerik:GridBoundColumn DataField="user_id" DataType="System.Int32"
                        HeaderText="User ID" SortExpression="user_id" UniqueName="user_id" ReadOnly="True">
                    <telerik:GridBoundColumn DataField="lastname" HeaderText="Last Name"
                        SortExpression="lastname" UniqueName="lastname" ReadOnly="False">
                    <telerik:GridBoundColumn DataField="firstname" HeaderText="First Name"
                        SortExpression="firstname" UniqueName="firstname" ReadOnly="False">
                <EditFormSettings CaptionFormatString="Edit User ID: {0}"  CaptionDataField="user_id" PopUpSettings-Modal="True" PopUpSettings-ScrollBars="Auto"
                EditFormType= "AutoGenerated">
                    <FormTableItemStyle Width="100%" Height="29px"></FormTableItemStyle>
                    <FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2"></FormTableStyle>
                    <FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
                    <EditColumn ButtonType="PushButton" />
<PopUpSettings ScrollBars="Auto" Modal="True"></PopUpSettings>
             <ClientSettings AllowDragToGroup="True">
                <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                <ClientEvents OnRowDblClick="RowDblClick" />
    <!-- content end -->

Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Globalization
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Telerik.Web.UI
Partial Class Admin_TeacherRole
    Inherits System.Web.UI.Page
    Private Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
        If Not IsPostBack Then
            For Each item As GridItem In RadGrid1.MasterTableView.Items
                If TypeOf item Is GridEditableItem Then
                    Dim editableItem As GridEditableItem = CType(item, GridDataItem)
                    editableItem.Edit = True
                End If
        End If
    End Sub
    Private Sub Page_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        If Not MyBase.IsPostBack Then
            Me.RadGrid1.MasterTableView.Items(0).Edit = True
        End If
    End Sub
    Public ReadOnly Property UserData() As DataSet
            Dim obj As Object = Me.Session("UserData")
            If Not obj Is Nothing Then
                Return CType(obj, DataSet)
            End If
            Dim MyUserData As DataSet = New DataSet
            Dim ConnString As String = ConfigurationManager.ConnectionStrings("ProLearnConnectionString").ConnectionString
            Dim conn As SqlConnection = New SqlConnection(ConnString)
            Dim adapter As SqlDataAdapter = New SqlDataAdapter
            adapter.SelectCommand = New SqlCommand("SELECT user_id, lastname, firstname FROM tblStudents ORDER BY user_id", conn)
            adapter.Fill(MyUserData, "tblStudents")
            Me.Session("UserData") = MyUserData
            Return MyUserData
        End Get
    End Property
    Private Sub RadGrid1_NeedDataSource(ByVal source As System.Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        RadGrid1.DataSource = Me.UserData
        Me.UserData.Tables("tblStudents").PrimaryKey = New DataColumn() {Me.UserData.Tables("tblStudents").Columns("user_id")}
    End Sub
    Protected Sub RadGrid1_UpdateCommand(ByVal source As System.Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand
        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
        Dim editMan As GridEditManager = editedItem.EditManager
        Dim column As GridColumn
        For Each column In e.Item.OwnerTableView.Columns
            If TypeOf column Is IGridEditableColumn Then
                Dim editableCol As IGridEditableColumn = CType(column, IGridEditableColumn)
                If (editableCol.IsEditable) Then
                    Dim editor As IGridColumnEditor = editMan.GetColumnEditor(editableCol)
                    Dim editorType As String = CType(editor, Object).ToString()
                    Dim editorText As String = "unknown"
                    Dim editorValue As Object = Nothing
                    If (TypeOf editor Is GridTextColumnEditor) Then
                        editorText = CType(editor, GridTextColumnEditor).Text
                        editorValue = CType(editor, GridTextColumnEditor).Text
                    End If
                    If (TypeOf editor Is GridBoolColumnEditor) Then
                        editorText = CType(editor, GridBoolColumnEditor).Value.ToString()
                        editorValue = CType(editor, GridBoolColumnEditor).Value
                    End If
                    If (TypeOf editor Is GridDropDownColumnEditor) Then
                        editorText = CType(editor, GridDropDownColumnEditor).SelectedText & "; " & CType(editor, GridDropDownColumnEditor).SelectedValue
                        editorValue = CType(editor, GridDropDownColumnEditor).SelectedValue
                    End If
                        Dim changedRows As DataRow() = Me.UserData.Tables("tblStudents").Select("user_id = " & editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("user_id"))
                        changedRows(0)(column.UniqueName) = editorValue
                    Catch ex As Exception
                        RadGrid1.Controls.Add(New LiteralControl("<strong>Unable to set value of column '" & column.UniqueName & "'</strong> - " + ex.Message))
                        e.Canceled = True
                    End Try
                End If
            End If
    End Sub
End Class

2 Answers, 1 is accepted

Sort by
Top achievements
Rank 1
answered on 27 Jul 2010, 02:24 AM
Any help? please -_-
Telerik team
answered on 29 Jul 2010, 07:39 AM
Hi Robin,

I examined the code you post and it is looks correct, however it is not runnable and I could not reproduce the described issue. Could you please send us a simple runnable project that could be used for reproducing the problem. You could open a formal support ticket from your Telerik account and attach a ZIP file there. Thus we will be able to gather more details about your scenario and provide you with more to-the-point answer.

Best wishes,
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Asked by
Top achievements
Rank 1
Answers by
Top achievements
Rank 1
Telerik team
Share this question