Results 1 to 5 of 5

Thread: How can i change IEnumerable to datatable

  1. #1

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    How can i change IEnumerable to datatable

    I used this but did not work :

    Code:
      Dim IENUM As IEnumerable = dat.getDanhSachDongSP
        Dim bid as New BindingSource        
        bid.DataSource = IENUM
        Dim dt2 As DataTable
         dt2 = bid.DataSource
    It show error "cant convert from IEnumberable to datatable"
    I see that error but i cant fix it. Help me please !!!!!
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How can i change IEnumerable to datatable

    You can't. You cannot simply change an object of one type into an object of another type. I don't know what the return type of dat.getDanhSachDongSP is but the DataTable class doesn't implement the IEnumerable interface so what you're trying to do is simply not possible. If you want a DataTable then you need to populate a DataTable. You can't just turn some other object that isn't a DataTable into one.

    So, what exactly DOES dat.getDanhSachDongSP return? If it's a DataView then you can get the DataTable it views from its Table property. I'm guessing that that's the case because, in order for that object to be assignable to the DataSource of a BindingSource, it must implement IList rather than just IEnumerable. That really begs the question, why are you assigning the result of dat.getDanhSachDongSP to an IEnumerable variable if it's an IList?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: How can i change IEnumerable to datatable

    No. Dat.GetdanhsachdongSP is IEnumerable type.
    I use LINQ and ADO.NET simultanousely to call storeprocedure.
    I use LINQ fatster ADO.NET so i used it.
    I want to get result datatable from that IEnumerable type .
    This is code :
    Code:
            public IEnumerable<prGetDanhSachDongSPResult> getDanhSachDongSP()
            {
                try
                {
                    QLBHDataContext data = new QLBHDataContext(getConnectionString()); ;
                    return data.prGetDanhSachDongSP();
                }
                catch (Exception ex)
                {
                    throw new Exception("Thông báo lỗi hệ thống:\n" + ex.Message);
                }
            }
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How can i change IEnumerable to datatable

    don't return an IEnumerable type... return a List<T> type instead... then add .ToList onto the end of your LINQ call... since it'sreturned as a list, it will implement the IList interface, which is what is necessary for you to bind it to the grid.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How can i change IEnumerable to datatable

    What's the point of using LINQ to SQL if you want a DataTable? LINQ to SQL is not faster than ADO.NET. If you want a DataTable then just use ADO.NET and you'll get a DataTable.

    That said, LINQ to SQL was invented because it offers many advantages over using ADO.NET directly. If you're going to use LINQ to SQL then use it and forget about DataTables.

    With that said, you're going to need an IList, not an IEnumerable, in order to use data-binding. You can do as tg says but I'd actually say that it's fine to return an IEnumerable and then convert that to an IList outside the getDanhSachDongSP method rather than inside it. You can do it either way, but consider this: a generic List doesn't provide the best support for data-binding. In order to have full data-binding support you need an IBindingList, which a generic List is not. If you want that full support then you'd need to create an ObservableCollection or maybe a BindingList, which you would almost certainly do outside your method, e.g.
    vb.net Code:
    1. Dim dataSource As New ObservableCollection(Of prGetDanhSachDongSPResult)(dat.getDanhSachDongSP())
    2.  
    3. bid.DataSource = dataSource
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width