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

Get all Grid values and pass them to a stored procedure

3 Answers 138 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 21 Jan 2009, 04:31 AM
How do I get all values displayed in my grid so that I can pass them as parameters for a stored procedure?

I have a radgrid that displays ID(PK), courseID, Course, and StudentID.  I have a button outside of the grid called RegisterAll.  If RegisterAll is clicked I would like to pass all of the displayed rows as parameters to a stored procedure that will insert them into a table. 
Thanks

3 Answers, 1 is accepted

Sort by
0
david
Top achievements
Rank 1
answered on 21 Jan 2009, 02:57 PM
figured it out -  make the rows 'selectable' and then in the code-behind through the selecled rows and pick up the primary keys

 

<ClientSettings EnableRowHoverStyle="True">

 

 

 

<Selecting AllowRowSelect="True" />

 

 

 

</ClientSettings>

 

 


protected

 

void button_clicked(object sender, EventArgs e)

 

{

 


 

int iCount = 0;

 

 

 

foreach (GridDataItem dataItem in RadGrid1.SelectedItems)

 

{

 

 

int ikey = (int)RadGrid1.SelectedItems[iSpamCount].OwnerTableView.DataKeyValues[RadGrid1.SelectedItems[iCount].ItemIndex]["CHID"];

 

....process keys....

 

 

 

 

iCount++; //get the next one

 

}

 

0
david
Top achievements
Rank 1
answered on 21 Jan 2009, 02:58 PM
nm 
0
davidp
Top achievements
Rank 1
answered on 21 Jan 2009, 05:10 PM

If I select one item from the grid and click register it works.  However if select all or more than one row I get the following error "Procedure or function Test2 has too many arguments specified."

VB

Protected Sub cmdSubmit_Click1(ByVal sender As ObjectByVal e As System.EventArgs) Handles cmdSubmit.Click  
            Dim sqlcon1 As SqlConnection = New SqlConnection  
            Dim sqlcom1 As SqlCommand = New SqlCommand  
            sqlcon1.ConnectionString = ConfigurationSettings.AppSettings("SiteSQLServer")  
            sqlcom1.CommandText = "[Test2]" 
            sqlcom1.CommandType = System.Data.CommandType.StoredProcedure  
            sqlcom1.Connection = sqlcon1  
            sqlcon1.Open()  
 
            Dim iCount As Integer = 0  
            For Each dataItem As Telerik.Web.UI.GridDataItem In RadGrid1.SelectedItems  
                Dim ID As Integer = DirectCast(RadGrid1.SelectedItems(iCount).OwnerTableView.DataKeyValues(RadGrid1.SelectedItems(iCount).ItemIndex)("ID"), Integer)  
                  
                System.Math.Max(System.Threading.Interlocked.Increment(iCount), iCount - 1)  
                Dim sqlID As New SqlParameter("@ID", SqlDbType.Int)  
                sqlID.Value = ID  
                sqlcom1.Parameters.Add(sqlID)  
                sqlcom1.ExecuteNonQuery()  
            Next 
 
            sqlcon1.Close()  
            sqlcon1.Dispose()  
        End Sub 

SQL
Insert Into Test (ID, Class, Date, [Time],[Location])  
select ID, Class, Date, [Time],[Location]  
from cerntrain_classes    
 
where ID = @ID 


SQL
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
david
Top achievements
Rank 1
davidp
Top achievements
Rank 1
Share this question
or