Solved

Unable to cast object of type System.Data.DataTable to type System.Collections.Generic.IEnumerable

Posted on 2016-09-05
3
41 Views
Last Modified: 2016-09-25
I am getting an error as follows:
Unable to cast object of type 'System.Data.DataTable' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataColumn]'. at ASP.views_home_index_aspx.__RenderindexContent(HtmlTextWriter __w, Control parameterContainer) in c:\Code\Temp\MvcApplication3\MvcApplication3\Views\Home\Index.aspx:line 96#
The code in Index.aspx is as follows:
<%
            if (Model != null)
            {
                Response.Write("95:model" + "<br />");
                foreach (DataColumn column in (IEnumerable<DataColumn>) Model)// error here: line 96
                {
                  Response.Write(column.ColumnName);
                }
                foreach (DataRow row in (IEnumerable<DataRow>) Model)
                {
                   foreach (object value in row.ItemArray)
                   {
                      Response.Write(value);
                   }
                }
            }
        %>

Open in new window

Thanks in advance for any help given.
0
Comment
Question by:home2shop
  • 2
3 Comments
 
LVL 6

Expert Comment

by:pritaeas
ID: 41784608
If Model is a DataTable, then it should have a Rows and Columns property. An example is here on SO.
0
 

Accepted Solution

by:
home2shop earned 0 total points
ID: 41784803
Thank you for the link, it was of some help.
In my controller action I had
var model = Session["dtTable"];
return View(model);

Open in new window

So now in the Index.aspx page I have the following that works.
<%
            DataTable dtTable = (DataTable) Session["dtTable"];
            if (dtTable != null)
            {
         %>
                <table style="color: GrayText; font-size: xx-small; border: 0px solid violet;">
                    <tr>
                        <%
                        foreach (DataColumn column in dtTable.Columns)
                        {
                        %>
                            <th>
                                <%=column.ColumnName%>
                            </th>
                        <%
                        }
                        %>
                    </tr>
                    <%
                    foreach (DataRow row in dtTable.Rows)
                    {
                    %>
                        <tr>
                            <%
                            foreach (object value in row.ItemArray)
                            {
                            %>
                                <td>
                                    <%=value%>
                                </td>
                                <%
                            }
                             %>
                        </tr>
                        <%
                    }
                     %>
                </table>
        <%
            }
        %>

Open in new window

0
 

Author Closing Comment

by:home2shop
ID: 41814555
found answer myself
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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