Click here to Skip to main content
15,887,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone,
I am Sujata recently using MVC2 in asp.net.Here i have to retrieve table data from sql inside the model and pass to view through controller.But I am facing problem inside the view page.

Please check my code inside CRUDModel,SaveController and Display View is as follows.

CRUDModel.cs
C#
public DataTable getAllEmployee()
        {
            
            DataTable dt = new DataTable();  
            string strConString =@"Data Source=.;Initial Catalog=Sujata;Integrated Security=True";  
            using (SqlConnection con = new SqlConnection(strConString))  
            {  
                con.Open();  
                SqlCommand cmd = new SqlCommand("Select * from Employee", con);  
                SqlDataAdapter da = new SqlDataAdapter(cmd);  
                da.Fill(dt);  
            }  
            return dt;
            //throw new NotImplementedException();
        }


Code inside SaveController.cs
C#
public ActionResult ShowRecord()
        {

            CRUDModel crud = new CRUDModel();
            DataTable dt = new DataTable();
            dt=crud.getAllEmployee();

            
            return View("Display",dt.Rows);
        }

Code on Display.aspx View as

ASP.NET
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcCRUDApplication.Models.CRUDModel>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Display</title>
</head>
<body>
  
    <h2>Display</h2>
    
   <input class="button" type="button" value="Clickme" onclick="location.href='<%=Url.Action("ShowRecord", "Save")%>'" />
  <div>
  
  <table border="1">  
                    <thead>  
                        <tr>  
                            <td>  
                                Name  
                            </td>  
                            <td>  
                                Employee ID  
                            </td>  
                            <td>  
                                Designation  
                            </td>  
                            <td>Department</td>  
                        </tr>  
                    </thead>  
                   <% foreach (System.Data.DataRow dr in Model.Rows)  
                    {  %> 
                        <tr>  
                       
                            <td> <%=dr["Name"].ToString() %>  </td>  
                            <td><%=dr["EmployeeId"].ToString()%>  </td>  
                            <td><%=dr["Desig"].ToString()%>  </td>  
                            <td><%=dr["Dept"].ToString()%>  </td>  
                            
                        </tr>  
                   <% } %>  
                  </table>
    </div>
    </body>

</html>





The above code on view page gives following error
C#
Compiler Error Message: CS1061: 'MvcCRUDApplication.Models.CRUDModel' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'MvcCRUDApplication.Models.CRUDModel' could be found (are you missing a using directive or an assembly reference?)


Please help me ...
thanks
Posted
Updated 10-Jan-16 23:17pm
v5

1 solution

From your controller you're returning the Rows property of a datatable as your model, which is something of type System.Data.DataRowCollection

DataTable.Rows Property (System.Data)[^]

DataRowCollection Class (System.Data)[^]

However your view page says it is looking for something of type CRUDModel. So change the view to expect a model of System.Data.DataRowCollection instead and change the code to;

ASP.NET
<% foreach (System.Data.DataRow dr in Model)  


However I'd also advise you to ditch MVC2 and aspx pages, that is very old tech, I'd move to a more up-to-date version of MVC and the Razor engine.
 
Share this answer
 
Comments
SujataJK 11-Jan-16 5:35am    
I am New to MVC concept thats why have begun from MVC2 .If i start directly from higher version then i never understand drowbacks of MVC2.I will do Some practice on MVC2 then i will move to higher version.


thanks for your advise.
As per your solution I did that but it gives another error like that...

foreach statement cannot operate on variables of type 'MvcCRUDApplication.Models.CRUDModel' because 'MvcCRUDApplication.Models.CRUDModel' does not contain a public definition for 'GetEnumerator'
SujataJK 14-Jan-16 0:08am    
Still I haven't solve this issue.Please help me
F-ES Sitecore 14-Jan-16 4:32am    
Amend your original question to add the code you're using.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900