Solved

Error in biding data of a table into a List<T> class.

Posted on 2014-12-26
4
185 Views
Last Modified: 2016-02-16
Hi:
I need to put my table "tblmyWebsite" data into List<T> class "List<myProductObject>
" and use LINQ to SQL.
I had encountered this err. See photo please.

myErr1.png
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace myWebApplication
{
    public partial class LINQ_List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            showProducts();
        }
        private void showProducts()
        {
            dbDataContext db = new dbDataContext();

            // the way to convert LINQ query to ArrayList
            List<myProductObject> myList = new List<myProductObject>

            //myList.AddRange(products).ToList();
            myList.AddRange(( from p in db.tblmyWebsites
                                select new
                                {
                                    p.Id,
                                    p.productName,
                                    p.tblprogrammingLanguage.programmingLanguage,
                                    p.tblapplicationType.applicationType,
                                    p.image,
                                    p.review,
                                    p.price
                                }).ToList());
        }
    }
}

Open in new window


and here is my Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace myWebApplication.App_Code
{
    public class myProductObject
    {
        public int Id { get; set; }
        public string productName { get; set; }
        public string programmingLanguage { get; set; }
        public string applicationType { get; set; }
        public string image { get; set; }
        public string review { get; set; }
        public int price { get; set; }

        public myProductObject(int Id, string productName, string programmingLanguage, string applicationType, 
            string image, string review, int price)
        {
            this.Id = Id;
            this.productName = productName;
            this.programmingLanguage = programmingLanguage;
            this.applicationType = applicationType;
            this.image = image;
            this.review = review;
            this.price = price;
        }
    }
}

Open in new window


and here is a snapshot of my table
table1.JPG
0
Comment
Question by:Mohammad Alsolaiman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 500 total points
ID: 40519357
To return myProductObject list, you need to specify myProductObject type in the select statement , thus please replace your LINQ query (lines 25  to 35 ) with the LINQ query shown below:
from p in db.tblmyWebsites
select new myProductObject( p.Id,
                                    p.productName,
                                    p.tblprogrammingLanguage.programmingLanguage,
                                    p.tblapplicationType.applicationType,
                                    p.image,
                                    p.review,
                                    p.price
                                )

Open in new window

0
 

Author Comment

by:Mohammad Alsolaiman
ID: 40519387
sorry Miguel
I had try it in two ways but still have an error!

No# 1:
err1
No#2:
err2
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40519538
Your actual error for #2 is in this line:
List<myProductObject> myList = new List<myProductObject>
because the line is not properly terminated. Please change this line as shown below:
List<myProductObject> myList = new List<myProductObject>();

Open in new window

0
 

Author Closing Comment

by:Mohammad Alsolaiman
ID: 40519592
I had seen a plenty of videos talking about using class with linq and List<T>.
The samples was always initiated hard coded objects .
I need someone to correct my information please.
Is it true that :
      I make "myProductObject" class.
      I select my "tblmyWebsites" records using "LINQ to SQL".
      I add selected records to a List< myProductObject > and name it "myList".
      Then I fill in the GridView1.DataSource = myList;
                  GridView1.DataBind();
      Is it appropriate approach to do it this way, while I'm able to fill in the GridView1.DataSource using LINQ immediately?
      What is the benefits with doing such a thing?

the code works good with me is:

private void showProducts()
        {
            dbDataContext db = new dbDataContext();

            // the way to convert LINQ query to ArrayList
            List<myProductObject> myList = new List<myProductObject>();

            //myList.AddRange(products).ToList();
            myList.AddRange(from p in db.tblmyWebsites
                      select new myProductObject( p.Id,
                                    p.productName,
                                    p.tblprogrammingLanguage.programmingLanguage,
                                    p.tblapplicationType.applicationType,
                                    p.image,
                                    p.review,
                                    p.price
                                ));
            GridView1.DataSource = myList;
            GridView1.DataBind();
        }

thanks a lot
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question