Stored Procedure to handle mulitple insert parameters send by asp.net webform

Posted on 2008-11-08
Medium Priority
Last Modified: 2012-05-05
Hi Experts,

I have an asp.net web form that passes a variable amount of parameters to insert into DB tables

It could be 2 parameters or one or 10 depending on the user

How do i declare the parameters in a stored procedure to accept these input parameters?


The web form asks users to enter contact names.. if more names are needed, dynamic textbox controls are created via button click

each contact name is added seperatly to an asp.net sqldatasource via insertparameters and it calls the sp to insert the values into the db

for the sp, i dont want to create 20 input parameters to cover all cases, say the user enters ten contact names(via ten seperate textboxes) on the webform

Create PROCEDURE test

@business_name varchar(50),
@business_code varchar(3),
@contact_name1 varchar(30),
@contact_name2 varchar(30),
@contact_name3 varchar(30),
@contact_name4 varchar(30),
@contact_name5 varchar(30),
@contact_name6 varchar(30),
@contact_name20 varchar(30)

should the multiple values be sent differently to the stored procedure?
Question by:miguel_j
  • 2
  • 2

Expert Comment

ID: 22915409
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22915462
in sql stored procedures there is one beautiful thing and that is setting the default values to the parameters

@business_name varchar(50) = '',
@business_code varchar(3) = '',
@contact_name1 varchar(30) = '',
@contact_name2 varchar(30) = '',
@contact_name3 varchar(30) = '',
@contact_name4 varchar(30) = '',
@contact_name5 varchar(30) = '',
@contact_name6 varchar(30) = ''

now while calling the stored procedure you can supply only the patameters where you have the value and rest of the parameters will be initilized with the default values defined

the following link further defines default parameters in sql
(you might have to register on the web site)

Author Comment

ID: 22915492
thank you for your help

So i do need to create many parameters to cover (not knowing how many times the user adds extra web controls..

i was wanting to create sp parameters based on how many parameters was sent from the web form? it will vary

say the user may enter 50 names? then taking those 50 names and adding them into a seperate rows using a loop?

Accepted Solution

moseack earned 750 total points
ID: 22915502
One other option (if you know that the values will only be strings) is to "serialize" the values into a single string, for example by Joining them with ';' as delimiter. Then splitting the value in the sproc (http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648).

String[] values = new[] {"one", "two", "three"};
String serString = String.Join(";", values);

Open in new window

LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 750 total points
ID: 22915684
i did not completely got your question previously

the best solution in this case should be passing and XML with object to the stored procedure and then reading the XML in the database and then looping it around and inserting or updating the multiple values to the database


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 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