Solved

Insert using a stored procedure

Posted on 2006-06-08
5
283 Views
Last Modified: 2012-05-05
Hello,

In my ASP.net Page I have a form for creating users. Below are the fields on the form:

First Name Textbox
Last Name  Textbox
User Id Textbox
Password Textbox
DOB Textbox
Locations Dropdown with multipleselect option

Submit Button

Except Locations all others are textboxes. Locations is a ListBox with SelectionMode=Multiple.

I have two tables User_Master and User_Location
What actually should happen is when the user hits submit First Name,Last Name, User Id, Password, DOB should get inserted into User_Master and the Primarykey(id) and the selected Locations ids  should get inserted into user_location table.

Can this be done with just one stored procedure. If yes, As Locations is multiple select option how can I pass the values to the stored procedure??

0
Comment
Question by:sureshraina
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Jojo1771
ID: 16867115
Could this be done. Maybe. Should it? -No

It would be  alot better if you  created 2 sp's one for the insert into master and one for location.

ie


Insert Master --Call SP to insert and have it  return the PK by doing  Select @@Identity as PK in your SP
Then loop thru your ddl
for each item as bah in list of ddl.items
Insert Loc(pass the pk from 1st SP if needed)--2nd SP
next

This is how I would do it.
0
 
LVL 15

Accepted Solution

by:
GavinMannion earned 500 total points
ID: 16867446
I would agree with Jojo1771, currently with SQL 2000 you cannot pass through complex datatypes like arrays.

So an ugly (and I mean very UGLY) hack would be to add 20 other params to the first storeprocedure to hold possible selected locations. Else you should do it Jojo1771's way.

The ugly hack would add absolutely no value and I would highly recommend against it.
0
 
LVL 2

Expert Comment

by:SKumar_1981
ID: 16868121
Try this
when u inserted in to master table write  a trigger in the master table to insert in to the detail table,

the trigger is

CREATE TRIGGER trig_insert ON master table name
FOR   insert
as

IF inserted(Location)
BEGIN
      Insert into  detailtablename
            lacation = insereted.location
            FROM INSERTED
            END

regards,
skumar
0
 
LVL 2

Expert Comment

by:SKumar_1981
ID: 16868129
Try this
when u inserted in to master table write  a trigger in the master table to insert in to the detail table,

the trigger is

CREATE TRIGGER trig_insert ON master table name
FOR   insert
as

IF inserted(Location)
BEGIN
     Insert into  detailtablename(location) values insereted.location
FROM INSERTED
          END

regards,
skumar
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16868232
The trigger would insert as you said, but I think the main question is how do you send through 5 locations with the same stored procedure? Remembering that sometimes it might only be 1 location and other times 20 locations.

I think this will have to be done with 2 sp's
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question