Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-09-05
3
Medium Priority
?
90 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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