Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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