Solved

Fill SQLDataAdaptor from IList

Posted on 2010-11-11
7
368 Views
Last Modified: 2012-06-21
I am using the property attached as the datasource for a RadGrid.

...datasource for grid...
 grdAvailableForSale.DataSource = PendingOrdersStore


I need to fill a SQLDataAdaptor with this data so I can insert all the records into a SQL Table

How do I fill the SQLDataAdaptor?
Protected Property PendingOrdersStore() As IList(Of Order)
        Get
            Try
                Dim obj As Object = Session("AFS_VB")
                If obj Is Nothing Then
                    obj = GetOrders()
                    Session("AFS_VB") = obj
                End If
                Return DirectCast(obj, IList(Of Order))
            Catch ex As Exception
                Session("AFS_VB") = Nothing
            End Try
            Return New List(Of Order)
        End Get
        Set(ByVal value As IList(Of Order))
            Session("AFS_VB") = value
        End Set

Open in new window

0
Comment
Question by:lrbrister
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:ajb2222
ID: 34112483
The SQLDataAdaptor is the connection between between the database and the datatable. It is used to fill the data table and to update the database with changes to the datatable.

The datatable is what you would set as the datasource of the control.
Here is an example

http://support.microsoft.com/kb/308055
0
 

Author Comment

by:lrbrister
ID: 34112706
ajb2222:
Yeah..I had found that and a few other examples.
Right now I have a datatable being filled with the code that is attached and I verified that the rows are there in the Watch WIndow
 
However...whan I'm trying to "fill" my SQLDataAdapter with this code...

Dim da As New SqlDataAdapter
da.Fill(GetSearch())
 
I am getting this error
"The SelectCommand property has not been initialized before calling 'Fill'."
Can someone help me out here?

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
            row("afsStatus") = s.afsStatus

            t.Rows.Add(row)
        Next

        Return t
    End Function

Open in new window

0
 
LVL 6

Expert Comment

by:ajb2222
ID: 34112852
if you are trying to update the sql server with what is in your datatable you need to use da.update(GetSearch())

you will also need to set the da.insertcommand  property.

I am assuming you created a connection to the database.


0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:lrbrister
ID: 34113340
ajb222,
man am I lost...
Attached is my insert stored procedure...
Here is my button click event...
What do I need to add/change/...fold spindle or mutilate to get this to work?
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection()
Dim da As SqlDataAdapter = New SqlDataAdapter
cn.ConnectionString = "Server=SQL02\DEV01;Database=northwind;UID=myid;PWD=mypwd;"
cn.Open()

da.Update(GetSearch())

cn.Close()
cn.Dispose()
End Sub

ALTER PROCEDURE [dbo].[sp_cfa_CollateralInsert] (
			@afsSource nvarchar(16),
			@afsSourceDetail nvarchar(3),
			@afsDate smalldatetime,
			@afsAmount money
)
AS
BEGIN
	INSERT INTO [PROC_CFA].[dbo].[testCollateral]
           ([afsSource]
           ,[afsSourceDetail]
           ,[afsDate]
           ,[afsAmount])
     VALUES
           (@afsSource,
			@afsSourceDetail,
			@afsDate,
			@afsAmount)
End

Open in new window

0
 
LVL 6

Accepted Solution

by:
ajb2222 earned 500 total points
ID: 34113698
Dim cn As New SqlConnection()
Dim da As SqlDataAdapter = New SqlDataAdapter
cn.ConnectionString = "Server=SQL02\DEV01;Database=northwind;UID=myid;PWD=mypwd;"
cn.Open()

' -- create the command
Dim command As SqlCommand  = new New SqlCommand( "dbo.sp_cfa_CollateralInsert", cn)
command.CommandType = CommandType.StoredProcedure

' -- add a line like this for each input value
command.Parameters.Add("@afsSource", SqlDbType.{datatype}, {length}, "afsSource")


' -- add command to dataadapter
da.InsertCommand = command

da.Update(GetSearch())


cn.Close()
cn.Dispose()
End Sub

0
 

Author Closing Comment

by:lrbrister
ID: 34115358
Great!  Definately put me on the right track...I'll attach my final code set
0
 

Author Comment

by:lrbrister
ID: 34115362
This is the final code set ...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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

820 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