Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

Telerik Grid append all rows to SQL

I have a Telerik TadGrid that is getting populated fine from SQL.

How can I take all the data in that grid and send it to a table in SQL?

There will be between 2 and 100 lines in the grids.
Public Sub getTestData()
        Dim objConn1 As SqlConnection
        objConn1 = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnPortal"))
        Dim oCom1 As SqlCommand
        oCom1 = New SqlCommand
        oCom1.Connection = objConn1
        oCom1.CommandText = "sp_cfa_TestCollateral"
        oCom1.CommandType = CommandType.StoredProcedure
        Dim dA As SqlDataAdapter = New SqlDataAdapter(oCom1)
        Dim myDataTable As DataTable = New DataTable()
        objConn1.Open()
        Try
            dA.Fill(myDataTable)
        Finally
            objConn1.Close()
        End Try
        grdCollateral.DataSource = myDataTable.DefaultView
        grdCollateral.GroupingSettings.CaseSensitive = False
       
        objConn1.Dispose()
        oCom1.Dispose()
        oCom1 = Nothing
    End Sub

Open in new window

Avatar of gamarrojgq
gamarrojgq

The destination table in SQL have the same columns that your Tadgrid? you are going to export all the columns from your Tadgrid?
Avatar of Larry Brister

ASKER

gamarrojgq
Yes
ASKER CERTIFIED SOLUTION
Avatar of gamarrojgq
gamarrojgq

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wonderful...got me on the right track.  I'll add my final code
Final solution is attached...thanks
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       
        Dim strSql As String
        'This will return you all columns from the table but no rows so you can add the new ones  
        strSql = "Select * From proc_cfa.dbo.testCollateral where 1 = 2"

        Dim dtDestination As New DataTable
        Dim dtReadAdapter As New SqlClient.SqlDataAdapter(strSql, System.Configuration.ConfigurationManager.AppSettings("ConnPortal"))
        dtReadAdapter.Fill(dtDestination)
        dtReadAdapter.Dispose()

        Dim gridTable As New DataTable
        gridTable = GetSearch()

        'Since you Grid was filled with a Store Procedure you need to import the rows to the Datatable manually  
        Dim drRow As DataRow
        For Each drRow In gridTable.Rows
            dtDestination.ImportRow(drRow)
        Next

        'This will save the Rows to your Table in SQL if there are no errors of primary/foreign key  
        Dim dtSaveAdapter As New SqlClient.SqlDataAdapter(strSql, System.Configuration.ConfigurationManager.AppSettings("ConnPortal"))
        Dim objCommBuild As New SqlClient.SqlCommandBuilder(dtSaveAdapter)
        dtSaveAdapter.Update(dtDestination)
        dtSaveAdapter.Dispose()


    End Sub

   
    Public Function GetSearch() As DataTable
        Dim list As IList(Of Order) = ShippedOrdersStore()
        Dim t As New DataTable()
        t.Columns.Add("afsSource", GetType(String))
        t.Columns.Add("afsSourceDetail", GetType(String))
        t.Columns.Add("afsDate", GetType(Date))
        t.Columns.Add("afsAmount", GetType(Double))

        For Each s As Order In list
            Dim row As DataRow = t.NewRow()
            row("afsSource") = s.afsSource
            row("afsSourceDetail") = s.afsSourceDetail
            row("afsDate") = s.afsDate
            row("afsAmount") = s.afsAmount

            t.Rows.Add(row)
        Next

        Return t
    End Function

Open in new window