Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Telerik Grid append all rows to SQL

Posted on 2010-11-11
5
Medium Priority
?
624 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:lrbrister
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 34113796
The destination table in SQL have the same columns that your Tadgrid? you are going to export all the columns from your Tadgrid?
0
 

Author Comment

by:lrbrister
ID: 34114354
gamarrojgq
Yes
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 2000 total points
ID: 34114580
Ok, you can try this


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 YOUDESTINATIONTABLE WHERE 1=2 "

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

        Dim gridTable As DataTable
        gridTable = CType(grdCollateral.DataSource, DataTable)

        '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()

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 34115349
Wonderful...got me on the right track.  I'll add my final code
0
 

Author Comment

by:lrbrister
ID: 34115353
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

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question