save null to sql using Linq to Sql
-
hi have following table student id name school_ID 1 john 5 2 sara 2 3 luije 4 school id name 1 area1 2 area2 3 area3 4 area4 5 area5 need to save "null" to school_ID with student ID(2)..?? thanks in advance
sounds like homework to me - what have you tried so far
Never underestimate the power of human stupidity RAH
-
sounds like homework to me - what have you tried so far
Never underestimate the power of human stupidity RAH
-
In your code, q is not a database row, but the query itself. Also, you are not selecting on student id 2. You should do something like this:
//query for student with id = 2
var q = from d in sdc.student
where d.id == 2
select d;//getting the first result or null if there is no student
var row = q.FirstOrDefault();//if there is a result, we update it
if (row != null)
{
row.school_ID = null;
}//saving changes to DB
sdc.SubmitChanges() -
In your code, q is not a database row, but the query itself. Also, you are not selecting on student id 2. You should do something like this:
//query for student with id = 2
var q = from d in sdc.student
where d.id == 2
select d;//getting the first result or null if there is no student
var row = q.FirstOrDefault();//if there is a result, we update it
if (row != null)
{
row.school_ID = null;
}//saving changes to DB
sdc.SubmitChanges()ok, got it solve but it is very straing this not work: -------
var q = (from d in sdc.student
where d.id == 2
select d).FirstorDefault();try {if (q != null) q.school_ID = null;} cathc{ }
------- following works:
int k = 0;
var q = (from d in sdc.student
where d.id == 2
select d).FirstorDefault();
// make exception Division by constant zero
try {if (q != null) k = 1/k;} catch { q.school_ID = null;}--------------------------- Really don't know why when write this inside catch{} it works if write outside not work Pleae check Thanks
-
ok, got it solve but it is very straing this not work: -------
var q = (from d in sdc.student
where d.id == 2
select d).FirstorDefault();try {if (q != null) q.school_ID = null;} cathc{ }
------- following works:
int k = 0;
var q = (from d in sdc.student
where d.id == 2
select d).FirstorDefault();
// make exception Division by constant zero
try {if (q != null) k = 1/k;} catch { q.school_ID = null;}--------------------------- Really don't know why when write this inside catch{} it works if write outside not work Pleae check Thanks