home » Finance » FinanceNews » How TO Update Data Using LINQ To SQL [ C#,...How TO Update Data Using LINQ To SQL [ C#, ASP.NET Programming]posted by jaitley(16) 14 months ago (www.techdreams.org)
I’m working with LINQ for the past few weeks. It is a very beautiful technology but with very limited documentation. Today I tried updating database tables with the data captured in a web form. The code we initially used to update the database tables with the help of LINQ to SQL is as follows <span style="color: green">//Initiate LINQ objects<br /> </span>pobjConsultant = <span style="color: blue">new </span><span style="color: #2b91af">t_consultant</span>();<br /> pobjAddress = <span style="color: blue">new </span><span style="color: #2b91af">t_address</span>();<br /> pobjContactDetails = <span style="color: blue">new </span><span style="color: #2b91af">t_contact</span>();<br /> pobjBankRecord = <span style="color: blue">new </span><span style="color: #2b91af">t_bank_record</span>();<br /> <span style="color: green">//<br /> //Gather data from form fields and assign values to the objects<br /> //….<br /> //….code for assigning values…<br /> //….</span> <span style="color: blue">using </span>(<span style="color: #2b91af">XITTimesheetDataContext </span>db = <span style="color: blue">new </span><span style="color: #2b91af">XITTimesheetDataContext</span>())<br /> {<br /> <span style="color: green">//<br /> </span>db.t_consultants.Attach(pobjConsultant);<br /> <span style="color: green">// our design does not have a FK between Consultant and address<br /> // as address table is used hold data from many other tables<br /> // like Consultant.<br /> </span>db.t_addresses.Attach(pobjAddress);<br /> db.t_bank_records.Attach(pobjBankRecord);<br /> db.t_contacts.Attach(pobjContactDetails);<br /> db.SubmitChanges();<br /> }The above code executes without any error but it does not update values in the database tables. After execution of the code none of the new values are updated to the database tables. Googling through forums revealed that we need to change the code as the following <span style="color: green">//Initiate LINQ objects<br /> </span>pobjConsultant = <span style="color: blue">new </span><span style="color: #2b91af">t_consultant</span>();<br /> pobjAddress = <span style="color: blue">new </span><span style="color: #2b91af">t_address</span>();<br /> pobjContactDetails = <span style="color: blue">new </span><span style="color: #2b91af">t_contact</span>();<br /> pobjBankRecord = <span style="color: blue">new </span><span style="color: #2b91af">t_bank_record</span>();<br /> <span style="color: green">//<br /> //Gather data from form fields and assign values to the objects<br /> //….<br /> //….code for assigning values…<br /> //….<br /> </span><span style="color: blue">using </span>(<span style="color: #2b91af">XITTimesheetDataContext </span>db = <span style="color: blue">new </span><span style="color: #2b91af">XITTimesheetDataContext</span>())<br /> {<br /> <span style="color: green">//<br /> </span>db.t_consultants.Attach(pobjConsultant);<br /> db.Refresh(System.Data.Linq.<span style="color: #2b91af">RefreshMode</span>.KeepCurrentValues, pobjConsultant);<br /> <span style="color: green">// our design does not have a FK between Consultant and address<br /> // as address table is used hold data from many other tables<br /> // like Consultant.<br /> </span>db.t_addresses.Attach(pobjAddress);<br /> db.Refresh(System.Data.Linq.<span style="color: #2b91af">RefreshMode</span>.KeepCurrentValues, pobjAddress);<br /> <span style="color: green">//<br /> </span>db.t_bank_records.Attach(pobjBankRecord);<br /> db.Refresh(System.Data.Linq.<span style="color: #2b91af">RefreshMode</span>.KeepCurrentValues, pobjBankRecord);<br /> <span style="color: green">//<br /> </span>db.t_contacts.Attach(pobjContactDetails);<br /> db.Refresh(System.Data.Linq.<span style="color: #2b91af">RefreshMode</span>.KeepCurrentValues, pobjContactDetails);<br /> <span style="color: green">//<br /> </span>db.SubmitChanges();<br /> }After adding db.Refresh() method calls before submitting the changes solved the problem and the values are updated in the database tables properly. Not sure about the drawbacks and implication of calling Refresh() method with KeepCurrentValues parameter. But solved my problem of the day. If you are aware of any implications please let me know.
Copyright © 2008 | Tech Dreams. If you are on a mobile phone, please check the new mobile edition at m.techdreams.org How TO Update Data Using LINQ To SQL [ C#, ASP.NET Programming] read more » |
|
Related Blog Posts
How TO Update Data Using LINQ To SQL [ C#, ASP.NET Programming]
- www.techdreams.org
iPhone OS 3.0 Update, Update iPhone Apps to iPhone OS 3.0
- www.iphoneapplicationdevelopmentindia.com
Programming ASP.NET AJAX: Build rich, Web 2.0-style UI with ASP.NET AJAX...
- forum.indimovi.com
Professional ASP.NET 2.0 Databases - DOWNLOAD eBOOK NOW..!
- bookfundas.com
Programming Visual Basic 2008
- bigdownload.blogspot.com
Asp.net Development Company India,Asp.net Programming India
- www.weblogixesolutions.com
PHP Web Development India PHP ASP.NET Application Development Web Programming...
- www.asiawebmedia.com
|
Advt |
AdvtCommentsAdd a comment
Post stamped by 1 Humsurfers |
|
Upcoming News
- Big Cinemas inaugurated its 15th..
- Mediapersons were bitten by the ..
- India resilient during wedding s..
- Coppola's 'Apoca..
- Apocalypse Now voted best film
- Big B to get Lifetime Achievemen..
- 'Apocalypse Now' voted &..
- Pranab Mukherjee hopeful of seve..
- Signs of a V-shaped recovery
- Trade deficit drops to $57.3 bn ..
- October exports fall 6.6%
- Exports decline by 6.6 per cent ..
- Exports decline by 6.6 pct in Oct
- Exports decline by 6.60 % in Oct..
- Oct exports down 6.6% y-o-y
- Sensex crosses 17K, Nifty 5K
- No cause for concern, says UAE c..
- Dubai World $26 bn debt plan soo..
- Recovery could push growth to 6...
- StanChart raises FY10 growth for..
Recent Comments
- Recruitment 2009 In Intelligence..
- Anonymous user
- Download R.S.AGRAWAL..
- Anonymous user
- Removal of gphone virus
- Anonymous user
- chat with indian girl..
- Anonymous user
- Exdesi Invitations for free....
- ANIL KUMAR
- Veer Salman Khan | Veer Movie..
- chitrita...
- Andhra Pradesh SBTET Diploma C05..
- Anonymous user
- Shivaji University Results..
- Anonymous user
- DU Date Sheet 2009 Delhi..
- Anonymous user
- chat with indian girl..



