Hussein Ojaghi
Hussein Ojaghi

Reputation: 2290

Retrieving info from database

I'm going to retrieve information from a database using LINQ but I don't know why I'm getting this error:

Invalid object name 'Retriveinfos'.

My class is here:

[Table (Name="Retriveinfos")]
public class Retriveinfo
{
        [Column]
        public string Name { get; set; }
        [Column]
        public string LastName { get; set; }
        [Column(IsPrimaryKey = true)]
        public int Id { get; set; }
    }

and then using this line of code to connect and retrieving information:

DataContext dcon = new DataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Second_School_project\Review_Site\Review_Site\App_Data\ReviewDatabase.mdf;Integrated Security=True;User Instance=True");
Table<Retriveinfo> Retriveinfos = dcon.GetTable<Retriveinfo>();

var c = from d in Retriveinfos
        where d.Id == 1
        select new { d.Name, d.LastName };

foreach (var a in c)
    Response.Write(a.Name.ToString() + " " + a.LastName.ToString());

Upvotes: 6

Views: 258

Answers (2)

Pleun
Pleun

Reputation: 8920

What happens if you type the dcon.xxx At that moment you should get intellisense and you get the right names.

Futhermore, there is no need to do the GetTable, you can just do it like this

var c = from d in dcon.Retriveinfos  
where d.Id == 1         
select new { d.Name, d.LastName };  

foreach (var a in c)   
{ ... }

By the way, if your ID is indeed unique you can do this:

var c = dcon.Retriveinfos.SingleOrDefault(s=>s.id == 1)

And you can also skip your foreach in that case

Upvotes: 0

Andy Stannard
Andy Stannard

Reputation: 1703

Well Retriveinfos is a table in your database not a class you may need to use something like the entity framework to create classes to represent you tables before you can do something like the above

Upvotes: 2

Related Questions