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

Get grid row values for insert into new table

3 Answers 160 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 13 Jan 2009, 02:28 AM
I have a grid that displays id, name, startdate, and enddate.  I have added a template column with a submit button.  When the submit button is clicked I need to add the data from that row to another table.  I am not sure how to get the values from the grid rows.  Any help will be appreciated.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Jan 2009, 04:09 AM
Hello David,

You can try out the following code to get the text for various cells in a row on clicking a button in the same row :
aspx:
<telerik:GridTemplateColumn UniqueName="TemplateColumn"
        <ItemTemplate> 
            <asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" /> 
        </ItemTemplate> 
        </telerik:GridTemplateColumn> 
        
        <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id"></telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name" ></telerik:GridBoundColumn> 
         

cs:
protected void SubmitButton_Click(object sender, EventArgs e) 
    { 
        Button btn = (Button)sender; 
        GridDataItem dataItem = (GridDataItem)btn.NamingContainer; 
        string strtxt = dataItem["Id"].Text; 
        string strtxt = dataItem["Name"].Text; 
         
    } 

Thanks
Princy.
0
David
Top achievements
Rank 1
answered on 14 Jan 2009, 05:50 AM
Princy,
I was able to use your code and make some progress.  I am able to pass the data as paramaters to a sql stored procedure to insert the data into the new table.  However, The data is getting wrote to the db on page refresh as well as when submit is clicked.  Here is my code. 

aspx

 

<telerik:RadGrid ID="RadGrid2" runat="server" DataSourceID="OnlineRegSections">  
<MasterTableView  DataKeyNames="SectionID">  
<Columns> 
<telerik:GridTemplateColumn UniqueName="TemplateColumn">    
            <ItemTemplate><asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="Submit_Click" /> 
            </ItemTemplate>    
        </telerik:GridTemplateColumn>    
        <telerik:GridBoundColumn DataField="CallNo" HeaderText="CallNo" SortExpression="CallNo" UniqueName="CallNo"></telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="CourseName" HeaderText="CourseName" SortExpression="CourseName" UniqueName="CourseName"></telerik:GridBoundColumn> 
          
          
        </Columns> 
</MasterTableView> 
</telerik:RadGrid> 

vb
Protected Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs)  
            Dim sqlcon1 As SqlConnection = New SqlConnection  
            Dim sqlCom1 As SqlCommand = New SqlCommand  
            sqlcon1.ConnectionString = ConfigurationSettings.AppSettings("SiteSqlServer")  
            sqlCom1.CommandText = "[SNP_OnlineRegAdd]" 
            sqlCom1.CommandType = System.Data.CommandType.StoredProcedure  
            sqlCom1.Connection = sqlcon1  
 
            'Get Logged in UserID  
            Dim objUserInfo As UserInfo = UserController.GetCurrentUserInfo  
            Dim sqlobjUserInfo As New SqlParameter("@objUserInfo", SqlDbType.Int)  
 
 
            'Get Datagrid Values and declare as sql param   
            Dim sqlSectionID As New SqlParameter("@SectionID", SqlDbType.Int)  
            Dim sqlDateRequested As New SqlParameter("@DateRequested", SqlDbType.Char, 10)  
            Dim btn As Button = DirectCast(sender, Button)  
            Dim dataItemKeys As Telerik.Web.UI.GridDataItem = DirectCast(btn.NamingContainer, Telerik.Web.UI.GridDataItem)  
            Dim SectionID As String = dataItemKeys("SectionId").Text  
 
            sqlobjUserInfo.Value = objUserInfo.UserID  
            sqlSectionID.Value = SectionID  
            sqlDateRequested.Value = Now  
 
            sqlCom1.Parameters.Add(sqlSectionID)  
            sqlCom1.Parameters.Add(sqlobjUserInfo)  
            sqlCom1.Parameters.Add(sqlDateRequested)  
 
            sqlcon1.Open()  
            sqlCom1.ExecuteNonQuery()  
            sqlcon1.Close()  
            sqlcon1.Dispose()  
 
        End Sub 

0
Shinu
Top achievements
Rank 2
answered on 14 Jan 2009, 10:09 AM
Hi David,

I guess this is an expected behaviour. When ever you refresh the page the last action will get executed again. You may go through the following link which discusses a similar scenario and see whether it helps to some extent.
Preventing Duplicate Record Insertion on Page Refresh

Thanks
Shinu
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
David
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or