Sachin R

Sachin R

  • NA
  • 91
  • 7.1k

Displaying Dataset values in Webgrid

Apr 11 2015 1:23 AM
.Can u please help me to over come this. 

Issue is column names are not recognized
source code

controller

    public class ADONETController : Controller
    {
        //
        // GET: /ADONET/
        
        public ActionResult Index()
        {
            Read r = new Read();

            //List<ADONET_DAL.Read> 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> 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<Read> dtlist = dt.AsEnumerable().Select(row => new Read
            {
                productname = row.Field<string>("productname"),
                price = row.Field<decimal>("price"),
                description = row.Field<string>("description")

            }).ToList();
            return dtlist;
        
        
    }
    }


view
@*@model List<TESTInternetMVC.ADONET_DAL.Read>*@
@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);




error:

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: 

Line 26:  Line 27:  Line 28: @g.GetHtml(tableStyle: "webgrid-table", Line 29:         headerStyle: "webgrid-header", Line 30:         footerStyle: "webgrid-footer",
 

Answers (3)