Issue is column names are not recognized
source code
controller
public class ADONETController : Controller
{
//
// GET: /ADONET/
public ActionResult Index()
{
Read r = new Read();
//List data = r.read().Rows.AsQueryable().
return View(r.read());
}
model
public class Read
{
public string productname;
public decimal price;
public string description;
public IEnumerable read()
{
DataAccess da=new DataAccess();
SqlConnection cn = DataAccess.connectionstring();
string q = "select productname,price,description from testinternetmvc..products";
SqlCommand cmd = new SqlCommand(q,cn);
cn.Open();
SqlDataAdapter sda=new SqlDataAdapter(q,cn);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
//ds.Clear();
//sda.Fill(ds.Tables[0]);
sda.Fill(dt);
//ds = cmd.ExecuteReader();
//SqlDataReader d = cmd.ExecuteReader();
cn.Close();
List dtlist = dt.AsEnumerable().Select(row => new Read
{
productname = row.Field("productname"),
price = row.Field("price"),
description = row.Field("description")
}).ToList();
return dtlist;
}
}
view
@*@model List*@
@using System.Data;
@{
WebGrid g = new WebGrid(source: Model,
//columnNames:null,
defaultSort:null,
rowsPerPage:2,
canPage:true,
canSort:true,
ajaxUpdateContainerId:null,
ajaxUpdateCallback:null,
fieldNamePrefix:null,
pageFieldName:null,
selectionFieldName:null,
sortFieldName:null,
sortDirectionFieldName:null);
}
@g.GetHtml(tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
caption:null,
displayHeader:true,
fillEmptyRows:false,
emptyRowCellValue:null,
columns:new[] {g.Column("productname"),g.Column("price"),g.Column("description")},
exclusions:null,
firstText:null,
previousText:null,
nextText:null,
lastText:null,
htmlAttributes:null);
Column "productname" does not exist.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.InvalidOperationException: Column "productname" does not exist.
Source Error:
|
Khargesh RajputPosted Apr 11, 2015, 10:15 PM
Try like.this with 2 parameters header and columnname.in grid.column
Sachin RPosted Apr 11, 2015, 4:09 PM
Thank you very much for the reply. The table exists actually with all those columns , also I could see on debugging step by step values are fetched from the table(from the model variable values I can see that) but only thing is that its not displayed..
PFB the query generated in SSMS for the table
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [id]
,[productname]
,[price]
,[description]
FROM [TESTInternetMVC].[dbo].[Products]
Khargesh RajputPosted Apr 11, 2015, 4:19 AM
select productname,price,description from testinternetmvc..products
in sql and check wheather
productname exists in table or not also check table
testinternetmvc..products