get dataset in view

avnish
avnish
Participant
242 Points
11 Posts

how to pass dataset from controller and get dataset in a view to display.

please explain it with example.

thanku in advance

 

Views: 11149
Total Answered: 4
Total Marked As Answer: 1
Posted On: 09-May-2015 22:03

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Hi Avnish,

There are two way to pass DataSet to view from Contrller

  • as Model
  • throgh ViewData

Passing the DataSet as Model:

Any object we can pass from cotroller in return View(Object) and we get this object by Model Variable. To get this object in View page we provide datatype of the object .In following example we pass DataSet object from controller as ds and in View @model System.Data.DataSet in Razor and in ASPX Inherits="System.Web.Mvc.ViewPage<System.Data.DataSet>"

In Controller: 

public ActionResult Index()
{
ViewData["Message"] = "Welcome to Nice One Code";
 
DataSet ds = new DataSet();
 
DataTable dt = new DataTable("NiceTable");
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
 
for (int i = 0; i < 3; i++)
{
 
DataRow row = dt.NewRow();
row["Col1"] = "col 1, row " + i;
row["Col2"] = "col 2, row " + i;
row["Col3"] = "col 3, row " + i;
dt.Rows.Add(row);
}
ds.Tables.Add(dt);//Better way get dataset from model class
 
return View(ds); //passing the DataSet as my Model
}

 In View:

RAZOR Engine:

@model System.Data.DataSet
@using System.Data;
<h2>Report</h2>
<table>
 
<thead>
 
<tr>
@foreach (DataColumn col in Model.Tables[0].Columns)
{
 
<th>@col.ColumnName</th>
}
 
</tr>
 
</thead>
 
<tbody>
@foreach (DataRow row in Model.Tables[0].Rows)
{
 
<tr>
@foreach (DataColumn col in Model.Tables[0].Columns)
{
 
<td>@row[col.ColumnName]</td>
}
 
</tr>
}
 
</tbody>
</table>

ASPX Engine:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Data.DataSet>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 
<title></title>
</head>
<body>
 
<h2><%: ViewData["Message"] %></h2>
 
 
<table border="1">
 
<thead>
 
<tr>
<%foreach (System.Data.DataColumn col in Model.Tables[0].Columns) { %>
 
<th><%=col.Caption %></th>
<%} %>
 
</tr>
 
</thead>
 
<tbody>
<%
foreach (System.Data.DataRow row in Model.Tables[0].Rows)
{ %>
 
<tr>
<%
foreach (var cell in row.ItemArray) {%>
 
<td><%=cell.ToString() %></td>
<%} %>
 
</tr>
<%} %>
 
</tbody>
</table>
</body>
</html>

 Through ViewData[""]:

In Controller: 

public ActionResult Index()
{
ViewData["Message"] = "Welcome to Nice One Code";
 
DataSet ds = new DataSet();
 
DataTable dt = new DataTable("NiceTable");
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
 
for (int i = 0; i < 3; i++)
{
 
DataRow row = dt.NewRow();
row["Col1"] = "col 1, row " + i;
row["Col2"] = "col 2, row " + i;
row["Col3"] = "col 3, row " + i;
dt.Rows.Add(row);
}
ds.Tables.Add(dt);//Better way get dataset from model class
 ViewData["ds"]=ds;
return View();
}

In View:

RAZOR Engine:

@using System.Data;
@{var ds=(System.Data.DataSet)ViewData["ds"];}
<h2>Report</h2>
<table>
 
<thead>
 
<tr>
@foreach (DataColumn col in ds.Tables[0].Columns)
{
 
<th>@col.ColumnName</th>
}
 
</tr>
 
</thead>
 
<tbody>
@foreach (DataRow row in ds.Tables[0].Rows)
{
 
<tr>
@foreach (DataColumn col in ds.Tables[0].Columns)
{
 
<td>@row[col.ColumnName]</td>
}
 
</tr>
}
 
</tbody>
</table> 

 

Posted On: 09-May-2015 14:18
avnish
avnish
Participant
242 Points
11 Posts
         

can we pass dataset as show below:

public ActionResult DataSet()

{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
DataSet ds = new DataSet();

SqlDataAdapter dap = new SqlDataAdapter("Select EmployeeID, EmployeeName from Employee", con);
con.Open();
dap.Fill(ds);
con.Close();


return View(ds);

}

*************************************************

if (answer==y)

{

please show me how to get in view

}

else

{

explain y??

}

 

 

Posted On: 10-May-2015 01:26
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Hi Avanish,

Yes you can as: 

RAZOR Engine:

@model System.Data.DataSet
@using System.Data;
<h2>Report</h2>
<table>
 
<thead>
 
<tr>
@foreach (DataColumn col in Model.Tables[0].Columns)
{
 
<th>@col.ColumnName</th>
}
 
</tr>
 
</thead>
 
<tbody>
@foreach (DataRow row in Model.Tables[0].Rows)
{
 
<tr>
@foreach (DataColumn col in Model.Tables[0].Columns)
{
 
<td>@row[col.ColumnName]</td>
}
 
</tr>
}
 
</tbody>
</table>
Posted On: 09-May-2015 15:05
avnish
avnish
Participant
242 Points
11 Posts
         

coolthanku very much..........it worked!!!

Posted On: 10-May-2015 01:45
 Log In to Chat