SirReadAlot
asked on
How to delete a record with linq
Hi Experts,
I wrote this code to help me delete records from a gridview when a user clicks on the delete button
so far it needs a select keyword somewhere.
Error 1 A query body must end with a select clause or a group clause
I wrote this code to help me delete records from a gridview when a user clicks on the delete button
so far it needs a select keyword somewhere.
Error 1 A query body must end with a select clause or a group clause
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
PubsDataContext context = new PubsDataContext();
var author = from a in context.authors
where a = a.au_id
context.authors.DeleteOnSubmit(author);
context.SubmitChanges()
if (e.Exception != null)
{
// ErrorMessageLabel.Text = "Failed to DELETE due to foreign key contstraint on the table. You may only delete rows which have no related records.";
e.ExceptionHandled = true;
}
}
LINQ - Select, Insert, Update, Delete data (gridview control)
http://bdotnet.in/blogs/lohithbn/archive/2008/07/27/linq-select-insert-update-delete-data-gridview-control.aspx
GridView Edit, Update, Delete, Paging, Sorting Using DLINQ
http://www.highoncoding.com/Articles/271_GridView_Edit__Update__Delete__Paging__Sorting_Using_DLINQ.aspx
http://bdotnet.in/blogs/lohithbn/archive/2008/07/27/linq-select-insert-update-delete-data-gridview-control.aspx
GridView Edit, Update, Delete, Paging, Sorting Using DLINQ
http://www.highoncoding.com/Articles/271_GridView_Edit__Update__Delete__Paging__Sorting_Using_DLINQ.aspx
ASKER
Hi, I have checked those linqs but what most they all demonstate is how to add, delete, etc hard coded column. ie.e delete where itemid is = to something.
I have no idea what items will be deleted or updated so that is why I am trying to use this code
protected void GridView1_RowDeleted(objec t sender, GridViewDeletedEventArgs e)
{
PubsDataContext context = new PubsDataContext();
author a = context.authors.Single(a => a.au_id == a.au_id);
context.authors.DeleteOnSu bmit(a);
context.SubmitChanges();
}
I am also following this example but want to use linq instead
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/detailsview.aspx
run the 3rd example.
I have no idea what items will be deleted or updated so that is why I am trying to use this code
protected void GridView1_RowDeleted(objec
{
PubsDataContext context = new PubsDataContext();
author a = context.authors.Single(a => a.au_id == a.au_id);
context.authors.DeleteOnSu
context.SubmitChanges();
}
I am also following this example but want to use linq instead
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/detailsview.aspx
run the 3rd example.
ASKER
protected void GridView1_RowDeleted(objec t sender, GridViewDeletedEventArgs e)
{
PubsDataContext context = new PubsDataContext();
author a = context.authors.Single(a => a.au_id == a.au_id);
context.authors.DeleteOnSu bmit(a);
context.SubmitChanges();
}
Error 1 A local variable named 'a' cannot be declared in this scope because it would give a different meaning to 'a', which is already used in a 'parent or current' scope to denote something else
how do i fix this
{
PubsDataContext context = new PubsDataContext();
author a = context.authors.Single(a => a.au_id == a.au_id);
context.authors.DeleteOnSu
context.SubmitChanges();
}
Error 1 A local variable named 'a' cannot be declared in this scope because it would give a different meaning to 'a', which is already used in a 'parent or current' scope to denote something else
how do i fix this
You are returning an IEnumerable ('list') of one authoer, not a single one, try using First(), FirstOrDefault(), SingleOrDefault() or Single()
I am using a lambda here because it is cleaner, but you can also wrap your statement in one:
var author = (from a in context.authors where a = a.au_id).First();
I am using a lambda here because it is cleaner, but you can also wrap your statement in one:
var author = (from a in context.authors where a = a.au_id).First();
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
PubsDataContext context = new PubsDataContext();
var author = from a in context.authors.First(a => a=a.au_id);
context.authors.DeleteOnSubmit(author);
context.SubmitChanges();
if (e.Exception != null)
{
//I wouldnt be so quick to assume this is the error, check it
// ErrorMessageLabel.Text = "Failed to DELETE due to foreign key contstraint on the table. You may only delete rows which have no related records.";
e.ExceptionHandled = true;
}
}
ASKER
thank naspinski,
but it says that
Error 1 A query body must end with a select clause or a group clause
but it says that
Error 1 A query body must end with a select clause or a group clause
I just looked at the query again, and it makes no sense...
where a = a.au_id
what is 'a' and where does it come from? It can't equal it's own au_id
where a = a.au_id
what is 'a' and where does it come from? It can't equal it's own au_id
ASKER
it was my attempt to
rewrite this
"DELETE FROM [authors] WHERE [au_id] = @au_id"
rewrite this
"DELETE FROM [authors] WHERE [au_id] = @au_id"
oh, ok, you are going to need to get 'au_id' from somewhere else - the method will not know what it is unless you tell it.
{
int au_id = 5;// make this into whatever id you want to delete
PubsDataContext context = new PubsDataContext();
var author = from a in context.authors.First(a => a.au_id = au_id);
context.authors.DeleteOnSubmit(author);
context.SubmitChanges();
if (e.Exception != null)
{
//I wouldnt be so quick to assume this is the error, check it
// ErrorMessageLabel.Text = "Failed to DELETE due to foreign key contstraint on the table. You may only delete rows which have no related records.";
e.ExceptionHandled = true;
}
}
ASKER
int au_id = 5;// make this into whatever id you want to delete==================>
this is the problem, the user will be deleting any item on the gridview. so i can's say item 1 0r 7
this is the problem, the user will be deleting any item on the gridview. so i can's say item 1 0r 7
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
.
ASKER
{
//try seting it to zero
PubsDataContext context = new PubsDataContext();
var author = from a in context.authors
where a = a.au_id
select a;
context.authors.DeleteOnSu
context.SubmitChanges();
if (e.Exception != null)
{
// ErrorMessageLabel.Text = "Failed to DELETE due to foreign key contstraint on the table. You may only delete rows which have no related records.";
e.ExceptionHandled = true;
}
}