Could not get this to work, When I Run project no console is displayed
also as I am new to Entity Framework with C# winforms, I would like to ask how to fill a TextBox with a context item.
I am using Visual studio 2010 version and MS SQL 2008 Express.
Thank you
Code:
using System;
using System.Data.Objects;
using System.Linq;
using System.Windows.Forms;
namespace MyNameSpace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnOk_Click(object sender, EventArgs e)
{
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
ObjectQuery
var query = from p in Employee
where p.FirstName == "Jane"
select new { p.ID, p.LastName, p.FirstName, p.Birth };
foreach (var ligne in query)
Console.WriteLine(ligne.ID + " " + ligne.LastName + " " + ligne.FirstName);
}
Console.Read();
}
}
}

VulpesRank #55 · 29.2K reputationPosted Mar 20, 2012, 2:11 AM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 19, 2012, 8:37 PM EDT
Code is correct to retrieve data, but it may possible your filter is not correct means "JANE " data is not in database please check it.
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
var query = from p in MyEntities.Employee
where p.Nom == "JANE"
select new { p.DepartmentId, p.EmployeeCode };
foreach (var ligne in query)
{
Console.WriteLine(ligne.ID + " " + ligne.LastName + " " + ligne.FirstName);
}
}
hope this help.
Samio170 reputationPosted Mar 20, 2012, 1:41 AM EDT
The record "JANE" is in the table, something else is making Console not work.
I add that I use Visual Studio 2010 with SP1.
And would like to use the same code to fill a TextBox in Form1, in stead of to use Console, to ensure from where the issue is coming.
Please could you make the code modification ?
Samio170 reputationPosted Mar 20, 2012, 2:45 AM EDT
Both of you are right, Finaly I could run it,
Please I would ask how to use the same code when looking for a single record. i.e. searching for TextBox1 content (Name) to retrieve Label1.Text (BirthDate).
Many Thanks
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 20, 2012, 2:51 AM EDT
nom is Name (full name) am I right?is it combination of Last name and first name?
If yes then I think keyword "contain" will help you.
Like:
var query = from p in MyEntities.Employee
where p.Nom.Contains("JANE")
select new { p.ID, p.LastName, p.FirstName, p.Birth };
After this Check Result in Query.
hope this help.
VulpesRank #55 · 29.2K reputationPosted Mar 20, 2012, 3:01 AM EDT
if (query != null) label1.Text = query.ToString();
Samio170 reputationPosted Mar 20, 2012, 3:28 AM EDT
I used your code and obtained "The name 'query' does not exist in the current context"
What have I to do ?
Samio170 reputationPosted Mar 20, 2012, 3:32 AM EDT
Sorry, it was my mistake, as the code was written using French words, and I omitted to translate that one.
Thank you anyway.
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 20, 2012, 3:56 AM EDT
Try
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities()) Employee = MyEntities.Employee;
{
ObjectQuery
var query = (from p in Employee
where p.FirstName == textBox1.Text.Trim()
select p.Birth).FirstOrDefault();
if (query != null) label1.Text = query.ToString();
}
hope this help.
VulpesRank #55 · 29.2K reputationPosted Mar 20, 2012, 3:56 AM EDT
Samio170 reputationPosted Mar 20, 2012, 5:52 AM EDT
May I use the same Topic or it should be better that I create a new topic ?
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 20, 2012, 7:25 PM EDT
It would grate if you create new topic.
thanks
Samio170 reputationPosted Mar 21, 2012, 3:46 AM EDT
thank you