0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oh! I've figured it out. The problem was in my Index.aspx the line Model.Rows[0].ItemArray[0].ToString(); should be =Model.Rows[0].ItemArray[0].ToString() it turns out that in displaying the model in view , semicolon should be removed and an = sign should be added first. hahaha it took me a while to figure it out LOL anyway thanks @Ajay Punekar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.