Solved

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

Posted on 2014-12-26
4
180 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

739 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