Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-12-26
4
Medium Priority
?
192 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 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 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