Insert Command - get data values

10 posts, 4 answers
  1. Bruce
    Bruce avatar
    33 posts
    Member since:
    Oct 2006

    Posted 21 Mar 2009 Link to this post

    How do I get the data vaues in the insert command?

    And after I save the data, how do I close the input window?

    Here's what I've tried:

        Protected Sub RadGrid_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
            'Get the GridEditFormInsertItem of the RadGrid       
            Dim insertedItem As GridEditFormInsertItem = TryCast(e.Item, GridEditFormInsertItem)
            ' Get an error on the next line
            Dim ProductName As String = (TryCast(insertedItem("ProductName").Controls(0), TextBox)).Text
            Dim UnitPrice As String = (TryCast(insertedItem("UnitPrice").Controls(0), TextBox)).Text
            Dim UnitsOnOrder As String = (TryCast(insertedItem("UnitsOnOrder").Controls(0), TextBox)).Text
            Dim insertQuery As String
            insertQuery = "insert into Products set "
            insertQuery = insertQuery & " ProductName='" & ProductName & "'"
            insertQuery = insertQuery & " ,UnitPrice=" & UnitPrice
            insertQuery = insertQuery & " ,UnitsOnOrder=" & UnitsOnOrder
            ' Execute the query
            GridDataBind()  ' Now that the data has been changed

        End Sub

  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 22 Mar 2009 Link to this post

    Hi Bruce,

    Have you set the EditMode to InPlace? If so try accessing the InsertFormItem as shown below.

    CS:
     
     
         Protected Sub RadGrid1_InsertCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
             
             Dim insertedItem As GridDataInsertItem = TryCast(e.Item, GridDataInsertItem) 
             ' Get an error on the next line 
             Dim ProductName As String = TryCast(insertedItem("ProductName").Controls(0), TextBox).Text 
             '......... 
            
         End Sub 
     
     


    Thanks
    Shinu
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bruce
    Bruce avatar
    33 posts
    Member since:
    Oct 2006

    Posted 23 Mar 2009 Link to this post

    EditMode is set to InPlace

    The suggested code returns a blank value when data has been entered.

  5. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 23 Mar 2009 Link to this post




  6. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 23 Mar 2009 Link to this post

    Hi Bruce,

    I am not sure why it is returning a blank value. Here is the code which I tried on my end which is working.

    VB:
     
     
     
         Protected Sub RadGrid1_InsertCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
             If (TypeOf e.Item Is GridDataInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then 
                 Dim insertItem As GridDataInsertItem = DirectCast(e.Item, GridDataInsertItem) 
                 Dim txtbx As TextBox = DirectCast(insertItem("ProductName").Controls(0), TextBox) 
                 Dim strProductName As String = txtbx.Text 
             End If 
         End Sub 
     
     


    Shinu



  7. Bruce
    Bruce avatar
    33 posts
    Member since:
    Oct 2006

    Posted 23 Mar 2009 Link to this post

    That did not work either.  It's probably something simple I overlooked. Here's my code:

    <%@ Page  EnableEventValidation="true"  Language="VB" AutoEventWireup="false" CodeFile="TestAdd.aspx.vb" Inherits="TestAdd" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
     <title>Sample Grid</title>
    </head>
    <body>
    <form id="form1" runat="server">
     <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
     </telerik:RadScriptManager>
     <telerik:RadGrid ID="radGrid" runat="server"
       GridLines="None"  
       PageSize="10"
       AutoGenerateColumns = "False"
       Skin="Office2007"
       OnItemCommand=  "radGrid_ItemCommand"
       OnItemDataBound="radGrid_ItemDataBound"
       OnDeleteCommand="RadGrid_DeleteCommand"
       OnUpdateCommand="RadGrid_UpdateCommand"
       OnInsertCommand="RadGrid_InsertCommand"
       OnItemCreated=  "RadGrid_ItemCreated"
       PagerStyle-Mode = "NextPrevNumericAndAdvanced"
     >
      <MasterTableView
         CommandItemDisplay="Top" 
         DataKeyNames="ProductID"
         AllowAutomaticInserts = "True"
         EditMode="InPlace"
         >
      <CommandItemTemplate >
         <div style="padding:10px 0px;">
           <asp:LinkButton ID="lnkcmdAdd" runat="server"     CommandName="InitInsert"><img height="25" width="25" style="border:0px;vertical-align:middle;" alt="" src="Images/AddRecord.gif" /> Add Record</asp:LinkButton>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         </div>
      </CommandItemTemplate>
       <Columns>
         <telerik:GridEditCommandColumn UniqueName="cmdEditColumn" />
       <telerik:GridBoundColumn ReadOnly="true" Visible="false" DataField="ProductID" UniqueName="ProductID" />
        <telerik:GridBoundColumn DataField="ProductName" DataType="System.String" HeaderText="Product Name"
         SortExpression="ProductName" UniqueName="ProductName">
        </telerik:GridBoundColumn>
        </Columns>
      </MasterTableView>
     </telerik:RadGrid>
     <asp:Label ID="lblMessage" runat="server"></asp:Label>
     </form>
    </body>
    </html>
    Imports CDSFunctions
    Imports Telerik.Web.UI
    Partial Public Class TestAdd
        Inherits System.Web.UI.Page
        Protected CDSSQLFunctions As New CDSFunctions.SQLFunctions()
        Protected SelectCommand As String = ""
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            '-----------------------------------------------------
            lblMessage.Text = ""
            If Not IsPostBack Then
                GridDataBind()
            Else
                GridDataBind()
            End If

        End Sub
        Public Sub GridDataBind()
            SelectCommand = "SELECT [ProductID],[ProductName],[CompanyName],[CategoryName],[QuantityPerUnit],[UnitPrice],[UnitsInStock] ,[UnitsOnOrder],[ReorderLevel],[Discontinued],[DateOfLastInventory],[DateLastUpdated] From vwProducts_Info order by ProductName"
            radGrid.DataSource = CDSSQLFunctions.GetDataSet(SelectCommand)
            radGrid.DataBind()
        End Sub

        Public Sub radGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs)
        End Sub
        Protected Sub radGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs)
        End Sub
        Protected Sub RadGrid_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)
        End Sub

        Protected Sub RadGrid_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
        End Sub
        Protected Sub RadGrid_UpdateCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
        End Sub
        Protected Sub RadGrid_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
            If (TypeOf e.Item Is GridDataInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then
                Dim insertItem As GridDataInsertItem = DirectCast(e.Item, GridDataInsertItem)
                Dim txtbx As TextBox = DirectCast(insertItem("ProductName").Controls(0), TextBox)
                Dim strProductName As String = txtbx.Text
            End If
        End Sub

    End Class

  8. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Mar 2009 Link to this post

    Hi Bruce,

    Try accessing the textbox value from the InsertForm in the ItemCommand event  as shown below.

    VB:
     
     
         Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
             If e.CommandName = "PerformInsert" Then 
                 If (TypeOf e.Item Is GridDataInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then 
                     Dim insertItem As GridDataInsertItem = DirectCast(e.Item, GridDataInsertItem) 
                     Dim txtbx As TextBox = DirectCast(insertItem("ProductName").Controls(0), TextBox) 
                     Dim strProductName As String = txtbx.Text 
                 End If 
             End If 
         End Sub 
     
     


    Also try to populate the Grid using Advanced Data Binding techniques.

    Thanks
    Princy
  9. Bruce
    Bruce avatar
    33 posts
    Member since:
    Oct 2006

    Posted 23 Mar 2009 Link to this post

    Same problem - no value for the textbox.

    Do you have an example that you could send me?

    Thanks.
  10. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 23 Mar 2009 Link to this post

    Hi Bruce,

    Give a try with the following code library that I have submitted.
    Manual Insert/Update/Delete operations using Auto-generated editform with sql statements from the code-behind
    Also refer the following help article.
    Insert/Update/Delete at database level with queries

    In the above given aspx code you have set the AllowAutomaticInserts property to true. Try setting it to false and see whether it helps.

    Thanks
    Shinu.
  11. Bruce
    Bruce avatar
    33 posts
    Member since:
    Oct 2006

    Posted 24 Mar 2009 Link to this post

    Thanks - It was my problem - I was binding the grid before checking the values.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017