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.