Solved

Fill SQLDataAdaptor from IList

Posted on 2010-11-11
7
366 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now