views:

102

answers:

2

I have this simple code but it shows error. I dont know where I am going wrong. I shows error in last line.."DeleteOnSubmit" linq_testDataContext db = new linq_testDataContext();

var remove = from aremove in db.logins
         where aremove.username == userNameString && aremove.Password == pwdString
         select aremove;
db.logins.DeleteOnSubmit(remove);

Thanks, Ani

+2  A: 

DeleteOnSubmit takes a single object. You are passing an IEnumerable<login> to it. Use DeleteAllOnSubmit instead, or select a single object from your resulting collection, like this:

var remove = (from aremove in db.logins 
             where aremove.username == userNameString 
                && aremove.Password == pwdString 
             select aremove).FirstOrDefault(); 

if(remove != null)
{
    db.logins.DeleteOnSubmit(remove);
}
Philip Daubmeier
@philip now query is working fine but data is not deleted from table. it is still there..how can I fix this..Thanks
Ani
Run the debugger and look what is in that `remove` variable. I guess there is no element inside it, therefore nothing is deleted.
Philip Daubmeier
By the way: why do you check against the password in your query? I guess the username alone is disjunct from all others, is it?
Philip Daubmeier
sir I am just testing this query weather its working or not..I am writing linq for the first time... I am having same problem with insert...my query does ont show any chnages in database...
Ani
I tried to use Debug by using F5 but it does not show any error..
Ani
thats difficult, because we neither know your database nor the contents of it. Did you set a breakpoint before starting the debugger and set a watch to the variable `remove`? What are the contents of that collection at runtime?
Philip Daubmeier
Sir I have a small table having three columns...userid (int), username (nvarchar(50)), Password (nvarchar(50))userid is primary key and is autoincrementing.how to see contents of collections at runtime..
Ani
Get started reading this: http://msdn.microsoft.com/en-us/library/sc65sadd.aspx. Basically you set a breakpoint on the line where you make the `RemoveAllOnSubmit` call, then you run the debugger. It holds the execution of the program at exactly that line. Now you can move your cursor over the `remove` variable. You expand that tree, clicking on 'results view', you either see some `login` objects, or just an empty collection.
Philip Daubmeier
ok..sir..yes i set the DeleteAllOnSubmit() line on breakpoint and now I am debugging it...
Ani
when I move my cursor over remove variable it shows this:(local varibale)IQueryable<login>remove
Ani
There is a plus sign right next to it, click it to expand the view. click the plus sign next to 'results view' and tell us what it shows there.
Philip Daubmeier
its empty..sir..
Ani
I mean results view is empty sir.
Ani
A: 

Instead of:

db.logins.DeleteOnSubmit(remove);

Call DeleteAllOnSubmit(), like this:

db.logins.DeleteAllOnSubmit(remove.ToList());

Make sure to call db.SubmitChanges() afterwards. You could use .AsEnumerable() as well, either or. If it's a large delete operation though, you may want to think about bypassing LINQ in this case.

Nick Craver
Thanks...yes its not showing any error but data is still in database...its not deleted...I refresh the database..still data is still there..how can I fix this
Ani
Good point Nick, I took it for granted to call `SubmitChanges` :)
Philip Daubmeier
I am calling submitchanges but still no change in database...I tried to run debugger(F5)...but shows no error...here's my code... var remove = from aremove in db.logins where aremove.username == "admin" db.logins.DeleteAllOnSubmit(remove); db.SubmitChanges();
Ani
@Ani - What value does `remove.Count()` have?
Nick Craver
sir i dont have remove.count(); how to see wht query is executing..
Ani
@Aani - Sorry, I meant to run it in the debugger when you're on the `DeleteAllOnSubmit()` line. You can execute it in the immediate window, use Ctrl+Alt+I to bring it up.
Nick Craver
I guess this is how i shd do....set the breakpoint on DeleteAllOnSubmit() line and then try to debug(F5)..isn't it sir??
Ani
@Ani - Correct, set a break point, when it lands on that line, Ctrl+Alt+I to open the immediate window, see what the result of `remove.Count()` is.
Nick Craver
immediate window is blank sir.
Ani
@Nick after setting break point on that line and debuggin it. when I move my cursor over remove variable it shows this: (local varibale)IQueryable<login>remove
Ani
I check the result view on remove variable on DeleteAllonSubmit.. it is empty..
Ani
@Ani - Then that's the problem, your query isn't finding anything to delete...so nothing deleted. Is it possible you need a case-insensitive query when selecting the user or something?
Nick Craver
I double check its...all lower case..just now i deleted my data source an created a new one...now I put a break point on datacontext line...and checnked if its null or not.. and its null..linq_testDataContext db = new linq_testDataContext();db is null...where i am doing wrong
Ani
datacontext itself is null..
Ani
Now I have data till this line...I checked with break point it has that on this like.. remove has that line that i want to delete...but still no changes in database. db.logins.DeleteAllOnSubmit(remove);after this line I am doing this. db.SubmitChanges();
Ani