Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
am getting error while displaying grid in asp.net mvc2. any one can help me?my requirement is to display editable gridview in asp.net mvc2 using ado.net entity model. my code follows as

ASP.NET
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<ienumerable><mymvctest.models.users>>" %>
 
<!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 id="Head1"  runat="server">
    <title>Index</title>
</head>
<body>
    <div>
        <table width="50%">
            <tr>
                <td width="10%" height="30px">
                    ID
                </td>
                <td width="20%">
                    User Name
                </td>
                <td width="10%">
                    Age
                </td>
                <td width="10%">
                    Gender
                </td>
            </tr>

            <%foreach (var item in Model)
              { %>
            <tr>
                <td>
                    <%=Html.DisplayFor(x=>item.ID) %>
                </td>
                <td>
                    <%=Html.DisplayFor(x => item.UserName)%>
                </td>
                <td>
                    <%=Html.DisplayFor(x => item.Age)%>
                </td>
                <td>
                    <%=Html.DisplayFor(x => item.Gender)%>
                </td>
            </tr>
            <%} %>
        </table>
    </div>
</body>
</html>

C#
public class GridController : Controller
        {
            //
            // GET: /Grid/

            public ActionResult Index()
            {
                DataTable dtGrid = new DataTable();
                MvcApplication3.Models.Grid.Users objGrid = new MvcApplication3.Models.Grid.Users();
                dtGrid = objGrid.GetGridData();

                List<users> Gridd = new List<users>();

                foreach (DataRow dr in dtGrid.Rows)
                {
                    MvcApplication3.Models.Grid.Users users = new MvcApplication3.Models.Grid.Users();
                    users.ID = Convert.ToInt32(dr["ID"]);
                    users.UserName = dr["Name"].ToString();
                    users.Age = Convert.ToInt32(dr["Age"]);
                    users.Gender = dr["Gender"].ToString();

                    Gridd.Add(users);
                }
                return View("Index", Gridd);
            }
        }


namespace MvcApplication3.Models
{
    public class Grid
    {
        public class Users
        {
            string ConnString = ConfigurationManager.AppSettings["ConnString"].ToString();
            DataSet ds;

            [Required]
            [Display(Name = "Id")]
            public int ID { get; set; }

            [Required]
            [Display(Name = "Name")]
            public string UserName { get; set; }

            [Required]
            [Display(Name = "Age")]
            public int Age { get; set; }

            [Required]
            [Display(Name = "Gender")]
            public string Gender { get; set; }


            public DataTable GetGridData()
            {
                try
                {
                    ds = new DataSet();
                    SqlConnection con = new SqlConnection(ConnString);
                    SqlDataAdapter ada = new SqlDataAdapter("Select Id,Name,Age,Gender from Emp", ConnString);
                    ada.Fill(ds);
                    return ds.Tables[0];
                }
                catch (Exception err)
                {
                    throw err;
                }
            }
        }
    }
}
Posted
v2
Comments
Jameel VM 24-Jun-13 5:33am    
what is the error?

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