• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

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

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?

e.g

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?
0
miguel_j
Asked:
miguel_j
  • 2
  • 2
2 Solutions
 
Refael AckermannCommented:
0
 
Anurag ThakurTechnical ManagerCommented:
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
http://www.sqlservercentral.com/articles/Stored+Procedures/usingparameterswithstoredprocedures/2004/
(you might have to register on the web site)
0
 
miguel_jAuthor Commented:
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?
0
 
Refael AckermannCommented:
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

0
 
Anurag ThakurTechnical ManagerCommented:
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

sample
http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now