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();
}
}
}
VulpesPosted Mar 20, 2012, 6:11 AM
SamioPosted Mar 21, 2012, 7:46 AM
thank you
Jignesh TrivediPosted Mar 20, 2012, 11:25 PM
It would grate if you create new topic.
thanks
SamioPosted Mar 20, 2012, 9:52 AM
May I use the same Topic or it should be better that I create a new topic ?
VulpesPosted Mar 20, 2012, 7:56 AM
Jignesh TrivediPosted Mar 20, 2012, 7:56 AM
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.
SamioPosted Mar 20, 2012, 7:32 AM
Sorry, it was my mistake, as the code was written using French words, and I omitted to translate that one.
Thank you anyway.
SamioPosted Mar 20, 2012, 7:28 AM
I used your code and obtained "The name 'query' does not exist in the current context"
What have I to do ?
VulpesPosted Mar 20, 2012, 7:01 AM
if (query != null) label1.Text = query.ToString();
Jignesh TrivediPosted Mar 20, 2012, 6:51 AM
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.
SamioPosted Mar 20, 2012, 6:45 AM
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
SamioPosted Mar 20, 2012, 5:41 AM
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 ?
Jignesh TrivediPosted Mar 20, 2012, 12:37 AM
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.