.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

linq - delete two tables not work

Posted By:      Posted Date: September 16, 2010    Points: 0   Category :ASP.Net
BurnbetDataContext db = new BurnbetDataContext(); var q = (from c in db.GetTable<tbl_Payment>() where c.UserId == "fc44c835-8e80-45ee-af84-68339a09c053" select c).Single<tbl_Payment>(); db.GetTable<tbl_Payment>().DeleteOnSubmit(q); db.SubmitChanges(); In App_Code I BurnbetDataContext.I have two tables and tbl_PaymentDetails tbl_PaymentAssociation is:Parent Child Class -->>> UserId UserId ClassData in Table tbl_PaymentDetails (child) does not delete.      

View Complete Post

More Related Resource Links

Sql Scripts - Delete all Tables, Procedures, Views and Functions


In a shared environment you typically don't have access to delete your database, and recreate it for fresh installs of your product. 

I managed to find these scripts which should help you clean out your database.

Use at your own risk.


Delete All Tables

--Delete All Keys


delete a row in datatable using linq to dataset

hihow can i delete a row in datatable using linq to dataset?I have a dataset in my project, need to delete some rows filtered by IDhow can i do?

Delete don't work strange

Hi, I have an xml file as follows:<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"> <Title>Products</Title> <link rel="self" href="http://localhost" /> <updated>01-09-2010T05:24:54Z</updated> <author> <Name>Yasmin</Name> </author> <id>tag:localhost,1-9-2010:/support/products</id> <entry Prd_ID="1111" Cat="CC" Type="0" xmlns="http://www.w3.org/2005/Atom"> <id>444</id> </entry> <entry Prd_ID="1222" Cat="CA" Type="0" xmlns="http://www.w3.org/2005/Atom"> <id>444</id> </entry> <entry Prd_ID="1542" Cat="CD" Type="1" xmlns="http://www.w3.org/2005/Atom"> <id>444</id> </entry> Now my problem is dat i want to delete all items with Type=0 I wrote this code but it doesnt work for some reason...  Dim doc As New XmlDocument() doc.Load(Server.MapPath("xml1.xml")) Dim rootnode As XmlElement = doc.DocumentElement Dim query As String Dim entry_info As XmlNodeList query = "entry[@Type='&

How to MAP Tables in Linq .dbml file

I am using VS 2010I've created LiNQ .dbml file via   .. Model>Add New Item>Data>LINQ to SQL ClasesI've put table on designer surface using drag & drop.I've done all this from one tutorial. There Is Mapping with arrows between tables in tutorial. How can i do this.. ???Can Any one Help Me Please..Thanks

Can't get Distinct work, when combining fields from inner joined tables

Hi to all.I have a users table, each user has a session and each session has multiple parameters.I would like to retrieve all params for a specific user and get the paramID from the params table and the date submitted from the sessions table.This is the linq to SQL query so far:var items = (from param in db.Params join session in db.Sessions on param.SessionID equals session.ID where userID.Equals(session.UserID) //userID is the function argument orderby session.DateSubmitted descending select new { ParamID = param.ID, DateSubmitted = session.DateSubmitted }).Distinct(); I have noticed that when the DateSubmitted is fetched, i have multiple instances of a param in the result set, meaning that the Distinct does not work as expected. If i remove it like this:var items = (from param in db.Params join session in db.Sessions on param.SessionID equals session.ID where userID.Equals(session.UserID) //userID is the fu

LINQ - Can't perform Create, Update or Delete operations on because it has no primary key.

Hi, not sure if I'm posting this thread in the right section. anyway, I''m trying to do the update/insert/delete using LINQ but the underlying table does not a primary key. The table consists of composite ket (e.g. fieldA & FieldB make a primary key). At the moment, I have LINQ complaning about not having a single primary key (not sure what LINQ does it - not making any sense at all). did anyone ever come across this issue and rectified it?  thanks

LINQ-to-SQL: How do you join more than 2 tables?

Hi there, I was trying to find out how I can join more than 2 tables using the LINQ-to-SQL syntax. For instance, joining 2 tables in SQL:  SELECT * FROM Table1 AS T1 INNER JOIN Table2 AS T2 ON T1.Key=T2.Column1WHERE T2.Key='17'; can be expressed as: var Result = from T1 in DbContext.Table1 join T2 in DbContext.Table2 on T1.Key equals T2.ForeignKey where T2.Key=17 select new { T1, T2 }; But how would I join 3 or more tables using LINQ? For example: SELECT * FROM (Table1 AS T1 INNER JOIN Table2 AS T2 ON T1.Key=T2.Column1) INNER JOIN Table3 AS T3 ON T3.Key=T2.Column2 WHERE T2.Key='37'; I've been searching and experimenting and I cannot seem to find any informraiton on this. One example I found involves putting the result for the frist join into a temp object, and then performing the second join. I'm not sure performance-wise if that's the same as doing a 3-table join directly using a single SQL statement. Any help will be greatly appreciated! Thanks,- K.  

How does Linq to Sql Determine which tables to write to

I was wondering how Linq to Sql determines which tables to write to when given primary/foreign Key relationships.  I can (hopefully) better explain using an example.  I have the following tables: Customer CustomerStatus CustomerType CustomerAddresses Therefore I have a Customer Entity, a CustomerStatus entity, a CustomerType Entity and a List of CustomerAddresses.  In my code, I load up the Customer Entity and the List of CustomerAddresses because that's all I need.  For some reason Linq to Sql is trying to insert a new Customer Status record.  I have no idea why its doing this.  Can anyone please explain to me how linq to sql determines which table(s) will be written to based on the entities above? Thanks

Is the delete records LINQ correct ?

I hope to delete all records in DBPrograms, is the following code correct?               DBContext db = DBContext(PublicDBPar.ConnectionString);            db.DBPrograms.DeleteAllOnSubmit(db.DBPrograms);            db.SubmitChanges();

Delete from multiple tables

 After trying myriad ways to accomplish deleting from two table, I found a way that actually works. However, AM am sure there is a better way to do this. Any ideas?  Imports System.Data Imports System.Data.SqlClient Imports Telerik.Web.UI Imports System.CodeDom Imports System.Web Partial Class Default2 Inherits System.Web.UI.Page Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sql As String Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("CF_SQL_Connection").ConnectionString() sql = "DELETE FROM Table1 WHERE IDTable1 = '5'" Dim connection As New SqlConnection(strConnString) Dim command As New SqlCommand(sql, connection) command.Connection.Open() command.ExecuteNonQuery() command.Connection.Close() Dim sql1 As String Dim strConnString1 As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("CF_SQL_Connection").ConnectionString() sql1 = "DELETE FROM Table2 WHERE IDTable1 = '5'" Dim connection1 As New SqlConnection(strConnString) Dim command1 As New SqlCommand(sql1, connection) command1.Connection.Open() command1.ExecuteNonQuery() command1.Connec

linq - delete dropdownlist

My code:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"DataSourceID="LinqDataSource1" DataTextField="UserId" DataValueField="UserId"></asp:DropDownList>BurnbetDataDataContext db = new BurnbetDataDataContext();        var selectedID = (DropDownList1.SelectedValue);        tbl_Payment toDelete = db.tbl_Payments.Single(p => p.UserId == selectedID);        db.tbl_Payments.DeleteOnSubmit(toDelete);        db.SubmitChanges();Error:System.InvalidOperationException was unhandled by user code  Message=Sequence contains more than one element  Source=System.Data.Linq  StackTrace:       at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)       at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)       at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IPr

MS Linq To SQL Samples - Insert/Update/Delete

Hi, I'm stumbling along trying my hand at Linq to SQL and have come across this code below that was provided by MS.http://msdn.microsoft.com/en-us/vbasic/bb737928.aspxThe problem is there is no ".add" method for Categories to chose in the statement db2.Categories.add(newCategory)I've seen similar videos here on ASP .NET that uses code very much like the example below, but I am unable to run this example connected to a Northwind SQL database using Web Developer 2010 Express. This is the One-to-Many example listed under Linq To SQL Samples - Insert/Update/Delete. Thanks for any help! Shawn Dim db2 = New DataClassesDataContext Dim ds = New DataLoadOptions() ds.LoadWith(Of Category)(Function(p) p.Products) db2.LoadOptions = ds Dim newCategory = New Category With {.CategoryName = "Widgets", _ .Description = "Widgets are the customer-facing analogues " & _ "to sprockets and cogs.", _ .Picture = Nothing} Dim newProduct = New Product With {.ProductName = "Blue Widget", _ .UnitPrice = 34.56#, _ .Category = newCategory, _

dataset - delete tables - Cascade

I have two table:- tbl_Payments- tbl_PaymentDetailsMy code:DataSet1TableAdapters.tbl_PaymentsTableAdapter cta = new DataSet1TableAdapters.tbl_PaymentsTableAdapter();cta.DeleteQuery();Dataset query:DELETE FROM tbl_PaymentsFROM            tbl_Payments INNER JOIN                         tbl_PaymentDetails ON tbl_Payments.ID = tbl_PaymentDetails.PaymentIdRelation:Key columns (tbl_Payments) | Foreign Key Columns (tbl_PaymentDetails)ID                                          | PaymentIdChoose what to createSelect: Foreign Key Constraint OnlyUpdate Rule: CascadeDelete Rule: CascadeWhy delete only the data from the first table?Data from other tables remain.Please help.LangDetecten>sl GoogleDicCCascade

Delete records if it is not getting inserted and also in the related tables

Hi All, If the insert into table person is successful, but the insert into table subject fails, delete the person record and all records associated with the person that were also inserted. If the insert into table person is successful, and the insert into subject is successful, but the insert into the booking table fails, delete the subject record, the person record ,and all records associated with the person that were also inserted. The reason for the failure into the 3 tables (person, subject, booking) should be logged to tblErrors . Also, a message should be written to tblErrors which logs the fact that this situation has occurred.     -- I have few stored procs which does inserts and updates How would i capture the record which is not getting inserted and log in to log table and delete the previous records which got inserted  which are related to that.

How can you work with listviews populated by linq to sql?



I'm having trouble with something that should be very simple.

I can populate a listview with a linq to sql control using the wizard, but actually need to do a more complicated query, so I eventually gave up as it wasn't at all obvious how I could add my own customer query (other than the very basic select statements through the wizard).

So I went the alternative route and wrote a linq to sql query and set that as the datasource.

The problem I now have is that I'm struggling to get edit functionality working, as although I've managed to get a button control into the form, I can't get it to make the listview go into edit mode for the selected record.

I suppose my first query is, was I right to go the way of creating my own linq to sql query in the codebehind or should I have done it in the linq to sql datasource some how?

Secondly, I've got the following code, that should bring up a message box when the button is clicked but it does not, can anyone explain why?

    Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
        If e.CommandName = "Edit" Then
        End If
    End Sub

The code for the butto

how to delete records from multiple tables.


Hi Friends,Tell me How to delete records from multiple tables by using a single query.

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend