How to Create a List from DataSet

I am trying to mimic code that is creating of List of objects and then passing it into a gridview datasource. The current code is hard coded and it works but now I want to use a SQL Server database dataset that fills the same object(NameObject) and I need to put it into the same format as the list that is being hardcoded. Im doing it this way so I can pass anything in and it will accept the converted "whatever" and sets it to a list and it works.

Here is what is working now

        If Not Page.IsPostBack Then
            If nameobj Is Nothing Then
                nameobj = New List(Of NameObject) From _
                    { _
                        New NameObject() With {.Name = "IPhone2", .State = "Apple Product 1", .City = 499.99D, .Street = "High", .AccidentDate = "31 March 2010"}, _
                        New NameObject() With {.Name = "IPhone6", .State = "Apple Product 2", .City = 399.99D, .Street = "Medium", .AccidentDate = "31 March 2011"}, _
                        New NameObject() With {.Name = "IPhone3", .State = "Apple Product 3", .City = 599.99D, .Street = "Medium", .AccidentDate = "31 March 2010"}, _
                        New NameObject() With {.Name = "IPhone3G", .State = "Apple Product 4", .City = 299.99D, .Street = "High", .AccidentDate = "31 March 2015"}, _
                        New NameObject() With {.Name = "IPhone4", .State = "Apple Product5", .City = 199.99D, .Street = "Medium", .AccidentDate = "31 Dec 2010"}, _
                        New NameObject() With {.Name = "IPhone5", .State = "Apple Product6", .City = 399.99D, .Street = "Low", .AccidentDate = "31 March 2010"}, _
                        New NameObject() With {.Name = "IPhone2", .State = "Apple Product7", .City = 459.99D, .Street = "High", .AccidentDate = "31 March 2010"}}
            End If

            myGV.DataSource = nameobj.ToList
            myGV.DataBind()

Open in new window


what I want to do is call a method to fill the NAMEObject with a stored procedure and be able to pass it in the same way it is happening above.
Something like this...

        Dim nameobj As New List(Of NameObject)  'This will call the method that has the stored proc and I want to fill the nameobj

        If nameobj Is Nothing Then
            myGV.DataSource = nameobj.ToList
        End If

Open in new window


Im not sure if I create a Dataset or a DataTable or DataView that I can create it as a List? I have everything set up already but I dont know how to do this?

Thanks for any help with this!!
jknj72Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
DataSource property works for any of
A DataTable
A DataView
A DataSet
A DataViewManager
Any component that implements the IListSource interface
Any component that implements the IList interface

As you have just one set of data, you can use datatable, or convert datatable result to list and your current code will be used as is.
0
Fernando SotoRetiredCommented:
What technology are you using to bring the data back from DQL Server, standard ADO.Net to fill a DataTable object or something like Entity Framework?

Can you post the SQL query that is used to bring the data back?

You can load the data from the SQL Database directly without the need to make for example List(Of NameObject) or do you want to transfer the data into a List(Of NameObject)?
0
jknj72Author Commented:
Najam,
ok then I am gonna try and get the DataSet set and then create a DataTable and try to Convert it to a List. Not sure though how can I set the DataSource with my object(NameObject) .ToList? What I have now is not working so Im a little lost on how to set it as a List(Of NameObject) to the gridviews DataSource?

Fernando,
Im using the SqlClient and the SP code is below

 Public Function InsuredSearchCall(search As NameObject) As DataSet
        'Dim spNAME As String
        Dim workParam1 As New SqlParameter("@Insured_Name", SqlDbType.VarChar, 60)
        workParam1.Direction = ParameterDirection.Input
        workParam1.Value = search.Name.ToString

        Dim workParam2 As New SqlParameter("@Accident_Date", SqlDbType.Decimal)
        workParam2.Direction = ParameterDirection.Input
        workParam2.Value = CType(search.AccidentDateString, Decimal)

        Dim workParam3 As New SqlParameter("@City", SqlDbType.VarChar, 20)
        workParam3.Direction = ParameterDirection.Input
        IIf(search.City = "", workParam3.Value = Nothing, workParam3.Value = search.City)

        Dim workParam4 As New SqlParameter("@State", SqlDbType.Char, 2)
        workParam4.Direction = ParameterDirection.Input
        IIf(search.State = "", workParam4.Value = Nothing, workParam4.Value = search.State)

        conn = New Data.SqlClient.SqlConnection(connectionString)
        conn.Open()
        da.SelectCommand = New SqlCommand
        da.SelectCommand.Connection = conn
        da.SelectCommand.CommandType = CommandType.StoredProcedure

Open in new window


Thanks for both of your help
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Najam UddinCommented:
Gridview works fine with list, here you can find more detail.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
You state this, " I am gonna try and get the DataSet set and then create a DataTable and try to Convert it to a List" , You do not have to convert to a List. For example if you have a reference to the DataTable object then you can do this.
myGV.DataSource = DataTableInstanceName

Open in new window

or if you have a DataSet you can do this
myGV.DataSource =  DataSetInstanceName.Tables(0)  '' If one table in the DataSet

Open in new window

1
jknj72Author Commented:
Thats exactly what I needed Najam...

Thanks for both your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.