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
Solved

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

Posted on 2014-12-26
4
174 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
  • 2
  • 2
4 Comments
 
LVL 35

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 35

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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