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

Insert Command - get data values

9 Answers 510 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bruce
Top achievements
Rank 1
Bruce asked on 21 Mar 2009, 10:07 PM
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

9 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 23 Mar 2009, 04:46 AM
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
0
Bruce
Top achievements
Rank 1
answered on 23 Mar 2009, 10:58 AM
EditMode is set to InPlace

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

0
Accepted
Shinu
Top achievements
Rank 2
answered on 23 Mar 2009, 11:34 AM



0
Accepted
Shinu
Top achievements
Rank 2
answered on 23 Mar 2009, 11:34 AM
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



0
Bruce
Top achievements
Rank 1
answered on 23 Mar 2009, 12:14 PM
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

0
Accepted
Princy
Top achievements
Rank 2
answered on 23 Mar 2009, 12:48 PM
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
0
Bruce
Top achievements
Rank 1
answered on 23 Mar 2009, 01:07 PM
Same problem - no value for the textbox.

Do you have an example that you could send me?

Thanks.
0
Shinu
Top achievements
Rank 2
answered on 24 Mar 2009, 04:17 AM
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.
0
Bruce
Top achievements
Rank 1
answered on 24 Mar 2009, 10:54 AM
Thanks - It was my problem - I was binding the grid before checking the values.
Tags
Grid
Asked by
Bruce
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Bruce
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or