Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,

I have a form with 3 fields. 1 is card number which is dynamic that is user can add more card numbers when they click on add more button. And the other 2 fields are start date and end date.
What i want is when the user clicks submit button after filling up all details the values will pass through parameters in backend database and fetch the records and will display in frontend gridview.
But the problem is i am unable to pass multiple values through parameter.
Like if 3 card numbers and dates are submitted.
Sql query should be like
select * from table where cards in (@card) and date between @startdate and @enddate.
So in above example how can i pass multiple card numbers in a single parameter(@card) and i do not know that exactly how many card numbers will be submitted. They can be 3, 4, 5 and so on.
Please help.

What I have tried:

Googled alot about this and people have posted to use table valued parameters. But i am not sure how to use them and also do not know if they will work in this scenario as the number of cards is not fixed in this case.
Posted
Updated 25-Oct-16 1:47am

In your case it's numbers, so you could just pass it as a comma separated list:
C#
List<int> data = new List<int>() {1, 2, 3};
string sqlCmd = string.Format("SELECT * FROM MyTable WHERE Cards IN ({0}) AND [Date] BETWEEN @startdate AND @enddate", String.Join(",", data));
But that's not a good approach in general as it leaves you open to SQL injection.
This is one way round it: Using SqlParameter with SQL's IN Clause in C# | SvenBit - Sebastian Hadinata[^]
 
Share this answer
 
1. Create a SQLParameter @cardxml of XML type. Place all the user selected cards into the parameter in the format <value>1<value>2.....etc.
2. Use a query like this
'select * from table
where cards in (
select n.value('./text()[1]','int') from @cardsxml.nodes('./value') as v(n)
)
and date between @startdate and @enddate'
3. Preferably use a SP to pull this off. I am assuming you are using SQL Server as backend.

Other approaches-
Send a comma separated list of card values to the backend and then parse it in the backend using techniques described sql server - Split function equivalent in T-SQL? - Stack Overflow[^]
 
Share this answer
 
Here's how to use table-valued parameters. (above solutions can work as well)
1. Create a table type in your database:
CREATE TYPE CardTableType AS TABLE   
( Card VARCHAR(50)  )  

2. You create a stored procedure that accepts table-valued parameter:
SQL
CREATE PROCEDURE dbo. usp_GetCards 
    @Cards CardTableType READONLY  
    AS   
    SET NOCOUNT ON  
    Select ... Where Card in (Select card From @Cards)..

3. In your .net code:
C#
param = new SqlParameter("@Cards", dataTableCardType);
                param.TypeName = "CardTableType";
                param.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(param);


For more information: Use Table-Valued Parameters (Database Engine)[^]
 
Share this answer
 
Hello,

I think you were using Varchar Data type for CardID, so try to form query like this


SQL
Select * from table where cards in (@card) and date between @startdate and @enddate

@card should be '1','2','3'


this will works
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900