I'm terribly new to MVC and programming in general, and I'm having trouble loading data from a SQL Server database into my MVC3 application. I couldn't get it to work with a model, so I only have a controller and a view. The code looks like this:
Controller:
public ActionResult Datatable()
{
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["SampleDBConn"].ToString();
SqlConnection connect = new SqlConnection(conn);
connect.Open();
SqlCommand RID = new SqlCommand("SELECT Request_ID FROM SampleDB.dbo.Transactions", connect);
SqlCommand Tdate = new SqlCommand("SELECT Trans_Date FROM SampleDB.dbo.Transactions", connect);
SqlCommand MID = new SqlCommand("SELECT Merchant_ID FROM SampleDB.dbo.Transactions", connect);
SqlCommand Ttype = new SqlCommand("SELECT Trans_Type FROM SampleDB.dbo.Transactions", connect);
SqlCommand Tamt = new SqlCommand("SELECT Total_Amt FROM SampleDB.dbo.Transactions", connect);
DataTable dt = new DataTable("MyTable");
dt.Columns.Add(new DataColumn("Request_ID", typeof(SqlCommand)));
dt.Columns.Add(new DataColumn("Trans_Date", typeof(SqlCommand)));
dt.Columns.Add(new DataColumn("Merchant_ID", typeof(SqlCommand)));
dt.Columns.Add(new DataColumn("Trans_Type", typeof(SqlCommand)));
dt.Columns.Add(new DataColumn("Total_Amt", typeof(SqlCommand)));
DataRow row = dt.NewRow();
row["Request_ID"] = RID;
row["Trans_Date"] = Tdate;
row["Merchant_ID"] = MID;
row["Trans_Type"] = Ttype;
row["Total_Amt"] = Tamt;
dt.Rows.Add(row);
connect.Close();
return View(dt);
}
View:
@model System.Data.DataTable
@{
ViewBag.Title = "Datatable";
Layout = "~/Views/Shared/_Layout.cshtml";
}
< h2 > Datatable < /h2 >
< div id="header" >< /div >
< br / >
< style type"text/css" >
#DT1{display:inline}
< /style >
< table border="1" id="DT1" style="background-color: Lime" >
< thead style="background-color:Olive" >
< tr >
@foreach (System.Data.DataColumn col in Model.Columns)
{
< th >@col.Caption< /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>
</table>
The resulting table displays each column like this:
Request_Id
System.Data.SqlClient.SqlCommand
Where "Request_Id" is the Column header and "System.Data.SqlClient.SqlCommand" is where the data should be
I used this method from another stackoverflow solution: Displaying standard DataTables in MVC
The end goal of my application is to dynamically query my database via checkboxes or drop-down lists to display charts, and I'll also need to be able to export the data to an excel file. If anyone has any thoughts or suggestions on how I should approach this, it would be greatly appreciated.
I've been able to used a stored procedure to statically create a chart via this method:
@{
ViewBag.Title = "Chart1";
}
< h2 >Chart1< /h2 >
@{
var db = Database.Open("SampleDBConn");
var dbdata = db.Query("August2012byMerch");
var myChart = new Chart(width: 1100, height: 600, theme: ChartTheme.Green)
.AddTitle("Merchant Totals by Transaction Type: August 2012")
.AddSeries("Default",
xValue: dbdata, xField: "Transaction Type",
yValues: dbdata, yFields: "Total")
.Write();
db.Close();
}
Thank you!