?
Solved

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

Posted on 2012-03-24
6
Medium Priority
?
2,477 Views
Last Modified: 2012-08-13
Hi,

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.

Thanks.
0
Comment
Question by:shah36
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:Ashok
ID: 37761346
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.

HTH
Ashok
0
 

Author Comment

by:shah36
ID: 37761352
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.

thanks
0
 
LVL 13

Assisted Solution

by:Ashok
Ashok earned 2000 total points
ID: 37761429
Create stored procedure like this.....

USE [AdventureWorks]
GO


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


Testing.....

USE [AdventureWorks]
GO

DECLARE      @return_value int

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

SELECT      'Return Value' = @return_value

GO
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 13

Accepted Solution

by:
Ashok earned 2000 total points
ID: 37761468
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).

HTH
Ashok
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37763798
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.
0
 

Author Closing Comment

by:shah36
ID: 37860140
Thank you very much for your help. It worked really fine.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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