Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fill SQLDataAdaptor from IList

Posted on 2010-11-11
7
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

636 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