Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Geeks,

A Logic which I am doing in a complex way. I just need to execute this query in Stored Procedure
SQL
select Sizes,SUM(Quantity) from tbl_SizeBreakup where (Brand=@brand) and (Combo in ('1','2')) ...


The combo I must pass in SQL PARAMETER using in C# is
C#
DataSet dt = new DataSet();
    cmd = new SqlCommand();
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.CommandText = "sp_Accessories";
    cmd.Connection = con;

    cmd.Parameters.AddRange(
            new SqlParameter[] {
                new SqlParameter("@Mode",mode),
                new SqlParameter("@Combo",combo),
        }}


So If I pass 1 parameter, working as expected. The combo which I should pass is a string[](string array).The array length can be anything depends on the user selecting in UI.

My question is how to pass string[] to
C#
new SqlParameter("@Combo",combo)
Posted

You should pass combo (whatever it means) as: '1,2,3,..., 21,...N' instead of string array parameter. Than you'll be able to split values on comma. See: Using comma separated value parameter strings in SQL IN clauses[^]
 
Share this answer
 
Comments
Sriram Ramachandran 22-Jul-14 1:47am    
cmd.Parameters.AddWithValue("@color", parameters); The value of parameters would be '1','2' and this pass to SQL SP to @color.
select Sizes,SUM(Quantity) as Quantity from tbl_SizeBreakup where (Combo_Color in (@color)) Group By Sizes ...
So will this work??
Maciej Los 22-Jul-14 1:51am    
No! Please read my answer carefully and follow the link.
Sriram Ramachandran 22-Jul-14 2:01am    
Understood and worked ... Thanks....
Maciej Los 22-Jul-14 2:04am    
You're very welcome. Please, accept my answer as a solution (green button) to remove question from unanswered list.
If you mean SQL Server, pass it as a Table-valued parameter:
SQL Server 2008 User Defined Table Types and Table-Valued Parameters[^]
 
Share this answer
 
Hi Ramachandran,

You can just send the @Combo value parameter as "1,2,hj,lo,2".
and in sql stored procedure split these values with "," (Comma) and create a table with these values. after that you can write your query like
select Sizes,SUM(Quantity) from tbl_SizeBreakup where (Brand=@brand) and (Combo in (select value from @Values)) ...

here is a sample code what you have to do in stored procedure(suppose you have pass
new SqlParameter("@Combo","1,2,lo,54"))

SQL
declare @Values Table
(
RowId int Not Null Identity(1,1) Primary Key
,Value nvarchar(255) Not Null
) 
set @Delimiter=','

Declare @startPos smallint
,@endPos smallint

If (Right(@Combo, 1) != @Delimiter)
Set @Combo = @Combo + @Delimiter
Set @startPos = 1
Set @endPos = CharIndex(@Delimiter, @Combo)

While @endPos > 0
Begin
Insert @Values(Value)
Select LTrim(RTrim(SubString(@Combo, @startPos, @endPos - @startPos)))

-- remove the delimiter just used
Set @Combo = Stuff(@Combo, @endPos, 1, '')

-- move string pointer to next delimiter
Set @startPos = @endPos

Set @endPos = CharIndex(@Delimiter, @Combo) 
end

select Sizes,SUM(Quantity) from tbl_SizeBreakup where (Brand=@brand) and (Combo in (select Value from @Values)) 


I think you will get what you needed..
 
Share this answer
 
v2
Comments
Sriram Ramachandran 22-Jul-14 1:28am    
Can't I pass the Combo in C# as combo[] ??
Ayan Chalki 22-Jul-14 1:33am    
array is not there in sql server.you can declare only table in sql server(like array). so you can't pass something which is not in sql server.

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