IT Community - Software Programming, Web Development and Technical Support

[ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

This is a discussion on [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ? within the ASP and ASP.NET Programming forums, part of the Web Development category; [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ? Hi, I have one database SQL 2000 (or ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 05-26-2009, 12:34 AM
bluesky bluesky is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 667
bluesky is on a distinguished road
Default [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

[ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

Hi,

I have one database SQL 2000 (or 2005 it's not important but I need to be soluzion compatible with 2000) with 2 tables declared as follow:

TAB1 (this one is the master)
IDTAB1 primary key autoincrement integer
NOTE varchar(50)
TS timestamp field

TAB2 (this one is the detail)
IDTAB2 autoincrement primary key integer
FKIDTAB1 this one is the foreign key from master table
VERSION (this field it's particular I will describe later anyway it's integer)
TEXT (string)
TS timestamp field

The structure it's currectly set up as a DATASET with two table called TAB1 and TAB2 and I have a datarelation between TAB1 and TAB2 for rappresent MASTER/DETAIL relation.
The structure it's rappresented in one form by two DATAGRIDVIEW, the MASTER one bound with TAB1 and the DETAIL bound with the relation. I am using 2 dataadapter for the 2 tables.

The field VERSION it's difficult part for me:
if the detail it's the first one for the master and if in datagridview bound at TAB2 no one write other things then null then have to be set to 1, if the detail newly inserted it's not the first one then VERSION have to be set to AVG(VERSION)+1, otherwise have to be set as in the field user write in the VERSION cell of my datagridview.

Using 2 commandbuilder and using the HANDLER event for onrowupdated and onrowupdating of the TAB1 I can assign the currect ID to FKIDTAB1 in TAB2 using the executescalar @@IDENTITY so when I click on button save I launch the UPDATE for TAB1 and UPDATE for TAB2 and all working fine (not the field VERSION because I still never understand how to solve that, if anyone have a suggestion I will be happy about it too).But what if I want to use Stored Procedure ? How using Transaction ?

This is the part I am not able to accomplish,

I hope someone can help on me,
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 05-26-2009, 12:35 AM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

Hi,

Base on my understanding of your requirement. You have master/detail tables, when you insert new record into these two tables, you need to use transaction.

SqlTransaction

SqlTransaction Class (System.Data.SqlClient)

To use SqlTransaction, you should first open a connection then use connection.BeginTransaction. Do all your command in try block, if all commands successfully executed, you can call transaction.Commit(); If exception occurs, call transaction.Rollback();

To use Stored Procedure, I think you'd better contact Transact-SQL team
Transact-SQL Forum
They can help you to learn how to create stored procedure according to your business logical. And the version logical should in the stored procedure.

To call stored procedure in your winform application, you can use SqlCommand class, set its CommandText to the stored procedure name and CommandType to StoredProcedure.

SqlCommand.CommandType Property (System.Data.SqlClient)
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 05-26-2009, 12:38 AM
bluesky bluesky is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 667
bluesky is on a distinguished road
Default Re: [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

Hi,

Actually I ask if it's better to use 2 stored procedure, one per tables, separated, and if in this way it's better using transaction inside my stored procedure or using it outside in my VB appication, and what's the difference ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 05-26-2009, 12:39 AM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

Hi,

I think you should design it separately.

For example, you need to insert 1 record to the master table and 2 records to the detail table. You should put these three command in one transaction. After you successfully insert the master record. you can retrieve the id of that record, then use that id to insert detail records. When all the insert command successfully executed, call commit method.

If you need to insert 1 record to the master and N records to the detail. It's hard for me to do these N+1 command in one stored procedure. I'm not good at stored procedure, forgive me if some one can do it easily.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 05-26-2009, 12:55 AM
bluesky bluesky is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 667
bluesky is on a distinguished road
Default Re: [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

Hi,

the transact and commit transact I have to use inside the stored procedure or outside in my VB. application in your opinion ? Thanks again for your help.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 05-26-2009, 12:58 AM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

Hi all,

SqlTransaction is always used in your .NET code, not in the DB stored procedure. You can follow the link I gave you at the first post.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 07-13-2009, 02:08 AM
Pieter Stefanus Pieter Stefanus is offline
D-Web Trainee
 
Join Date: Apr 2009
Posts: 21
Pieter Stefanus is on a distinguished road
Default Re: [ADO.NET + VB 2005] Master Detail insert - one stored procedure or divided ?

First of all thanks for answer my question, actually I ask if it's better to use 2 stored procedure, one per tables, separated, and if in this way it's better using transaction inside my stored procedure or using it outside in my VB appication, and what's the difference ?
Thanks again, best regards

Last edited by arjkhanna : 07-13-2009 at 02:49 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure itbarota Database Support 3 03-10-2008 08:43 PM
Stored procedure gets executed twice chris ASP and ASP.NET Programming 4 11-01-2007 02:08 AM
Stored procedure gets executed twice chris VB.NET Programming 0 10-30-2007 07:55 AM
Can a stored procedure call itself or recursive stored procedure? How many level SP n H2o Database Support 1 08-03-2007 09:55 AM
Username,Password verfication in SQL Server 2005 itself using stored procedure oxygen Database Support 0 07-19-2007 11:21 PM


All times are GMT -7. The time now is 01:13 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0