Solved

Fill SQLDataAdaptor from IList

Posted on 2010-11-11
7
365 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql query for filter 12 23
Sql query to Stored Procedure 6 21
Recommendation vb6 to vb.net or others 14 44
select over clause 1 15
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

744 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

11 Experts available now in Live!

Get 1:1 Help Now