First tread on stack overflow, i will try to do my best..
Below, my model from ADO.NET (mysql database)
http://img585.imageshack.us/img585/4533/screenshot20120519at143.png
// CHECK USER+PASS
rmEntities2 myDb = new rmEntities2();
var query = from c in myDb.users
where c.use_login == run.name
where c.use_password == run.pass
select c.use_id;
if (query.Count() == 0)
{
return "ERROR USER/PASSWORD";
}
else
{
// Linq insert DB
running newRun = new running();
newRun.run_start_time = new System.DateTime(1970, 1, 1, 0, 0, 0, 0)
.AddSeconds(Double.Parse(run.start));
newRun.run_end_time = new System.DateTime(1970, 1, 1, 0, 0, 0, 0)
.AddSeconds(Double.Parse(run.stop));
newRun.run_distance = (float)run.distance;
newRun.run_nbfootsteps = run.nbfootsteps;
newRun.run_vo2max = run.vo2Max;
foreach (int u in query)
{
newRun.user.use_id = u;
}
myDb.AddTorunnings(newRun);
myDb.SaveChanges();
return "RUNNING ADDED";
}
The problem is from this line :
newRun.user.use_id = u;
I would like set up the run_use_id from user in my running table.
What is the best way?
I expect that when you try to set the
use_idvalue in the lineyou’re getting a
NullReferenceException. This is because newRun.user is null – the new run is not associated with a user.I’d suggest that instead of working with the
use_id, instead work with theuser.and then either of these:
They both do the same thing, but use whichever makes more sense to you.