Solved

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

Posted on 2016-09-05
3
39 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 24
asp enable view state 1 24
DataTables + iCheck + pagination Issue 2 23
asp.net input html code have it display the content 2 12
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

772 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