Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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