Dynamically add the checkboxlist checked value of asp.ne/C# in a stored procedure of sql server 2008


I am using Asp.net/C# 3 tiers Architecture, i have 3 checkboxlists and there are several checkboxes in each checkboxlist, im sending the parameters of every checked checkbox value to sqlserver 2008 as a parameter and then include it in the select statement.

the problem i have is im sending all checkboxes as a parameter and then in sql server stored procedure im checking with if statement that if this is not empty then include it in the select statement so for this i have included a lot of if statements,

my question is that is there any way to build a dynamically sql server stored procdeure which check the parameters that if it is not empty then it include it in the select statement otherwise ignore it.

any help will be appreciated.

Ali ShahSQL DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AshokSr. Software EngineerCommented:
Would not it be better to build field names separated by comma and pass it to the stored procedure?

For example, if checkbox1 is checked, checkbox2 is not checked, checkbox3 is checked and checkbox4 is checked, then

you pass "field1, field3, field4" to the stored procedure.  By the way the string you pass would become fully dynamic.

then in your stored procedure, the select statement would look something like.....
select field1, field3, field4 from mytable

Just an idea.

Ali ShahSQL DeveloperAuthor Commented:
thanks ashok, please can u give me an example as  i need to write the stored procedure in sql server , how can i make the string in asp.net/C# and then how do i convert that string in stored procedure and use it in the select statement in sql server.

AshokSr. Software EngineerCommented:
Create stored procedure like this.....

USE [AdventureWorks]

ALTER PROCEDURE [dbo].[TestFieldPass]
  @field_names varchar(500)
  set nocount on
  set ansi_warnings off
  declare @strSQL varchar(1500);
  set @strSQL = 'select ' + @field_names + ' from Person.Contact';


USE [AdventureWorks]

DECLARE      @return_value int

EXEC      @return_value = [dbo].[TestFieldPass]
            @field_names = N'Phone,EmailAddress'

SELECT      'Return Value' = @return_value

Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

AshokSr. Software EngineerCommented:
On ASP.NET side (assuming CheckBoxList1.Items[i].Text has fieldname)

string sql ='';

      for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        if (CheckBoxList1.Items[i].Selected == true)
         sql += CheckBoxList1.Items[i].Text + ",";

sql.Remove(sql.Length - 1, 1);

Open in new window

Now you can pass sql (as FieldNames parameter to your Stored Procedure).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
Please post your Stored Procedure as it stands and we can make more appropriate suggestions.  Typically you can pass all your checkbox values as a single parameter, either as a tinyint or Xml.
Ali ShahSQL DeveloperAuthor Commented:
Thank you very much for your help. It worked really fine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.