I have 1 WinForm and 2 tables:
- Form1 contains TextBox1
- Employee contains ID, LastName, FirstName, BirthDate.
- Tasks contains ID, TaskName
I need to input LastName and FirtName in TextBox1 to retrieve the related "TaskName" if exists,
then make the retrieved data usable from any location in the project
I need to know where to put the TaskName variable?
and how can I modify the next code to reach that purpose?
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
ObjectQuery
var query = (from p in Employee
where p.FirstName == TextBox1.Text.Trim()
select p.LasttName, p.FirstName);
}
Thanks
VulpesPosted Mar 21, 2012, 11:18 AM
string[] items = TextBox1.Text.Trim().Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
string lastName = items[0];
string firstName = items[1];
The second argument to the Split method deals with the possibility that there might be multiple spaces separating the last and first names.
If the ID column is used to link the Employee and Tasks tables, you should then be able to retrieve the related TaskName and place it in a global variable as follows:
// create a class to contain global variables if you don't already have one
static class Global
{
public static string Task {get; set;}
}
// within some method in some other class
// insert above code to get last and first names
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
ObjectQuery
ObjectQuery
Global.Task = (from e in Employee where e.LastName == lastName && e.FirstName == firstName join t in Tasks on e.ID equals t.ID select t.TaskName).FirstOrDefault();
}
// retrieve global variable from elsewhere:
if (Global.Task != null)
{
// do something with Global.Task
}
VulpesPosted Mar 22, 2012, 5:32 AM
I usually prefer to put my global variables in a separate Global class so I can easily find them but it's just a matter of taste.
SamioPosted Mar 21, 2012, 5:22 PM
I tested your solution, it's good.
Do you think it should be better if I place the Global Variable in Program,
or maybe in Form1 before: "public partial class Form1 :Form"