Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now