Question

I am creating an asp.net mvc2 project. I wanted to display data from a datatable but when i run the project, it does not display the data i wanted to display.

these are my codes:

HomeController.cs

    public ActionResult Index()
    {
        connection connect = new connection();
        string query = "SELECT Event_Name FROM tbl_Event WHERE Event_ID=2";

        return View(connect.SelectRecord(query));
    }

Index.aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Data.DataTable>" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Index
    </asp:Content>

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

        <h2>Index</h2>
        <%Model.Rows[0].ItemArray[0].ToString(); %>

    </asp:Content>

connection.cs

        internal DataTable SelectRecord(string query)
        {
            try
            {
                OpenConnection();
                cmd = new SqlCommand(query, conn);
                adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cmd = null;
                CloseConnection();
            }

        }

I wanted to display this line from Index.aspx <%Model.Rows[0].ItemArray[0].ToString(); %>

I have tried the answer of Kurt Schindler in this link: Displaying standard DataTables in MVC but it does not specify how to display a specific data from the datatable. Please help me. The reason I use datatable to display data on asp.net views is because I am comfortable using it as my temporary storage of data.

Was it helpful?

Solution

Try this

public ActionResult Index()
{
    connection connect = new connection();
    string query = "SELECT Event_Name FROM tbl_Event WHERE Event_ID=2";

    return View(connect.SelectRecord(query));
}
internal DataTable SelectRecord(string query)
{
        try
        {
            OpenConnection();
            cmd = new SqlCommand(query, conn);
            adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd = null;
            CloseConnection();
        }

}

Here is view

View: (strongly typed as System.Data.DataTable)

<table border="1">
<thead>
    <tr>
        <%foreach (System.Data.DataColumn col in Model.Columns) { %>
            <th><%=col.Event_Name%></th>
        <%} %>
    </tr>
</thead>
<tbody>
<% foreach(System.Data.DataRow row in Model.Rows) { %>
    <tr>
        <% foreach (var cell in row.ItemArray) {%>
            <td><%=cell.ToString() %></td>
        <%} %>
    </tr>
<%} %>         
</tbody>

Check this link : http://weblogs.asp.net/gunnarpeipman/archive/2011/11/19/asp-net-mvc-simple-view-to-display-contents-of-datatable.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top