Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

i need some help with passing List values (or so I believe it's List :) ) to parameter value of sql procedure I have in sql server

trying to explain:

I have datagridview with one column called sampleID, I have 10 rows of data in datagridview and in sampleID i have single values of numbers
1
3
3
3
5
5
7
7
9


I have textBox1 where user which is logged on windows form holds numbers which he is allowed to see in the form, and let's say that I can only see rows that holds 3 and 7
so in textBox1 I divided it like this

,3,,7,

now my sql procedure is simple

SQL
USE [FreeDatabase]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[sample_proc]

@PageSize int output, 
@PageNum int output,
@sampleID varchar(MAX) output

as

Declare @RowStart int 
Declare @RowEnd int 

--SELECT	@PageSize = 50,
	--@PageNumber = 1
	
if @PageNum > 0 
Begin 

SET @PageNum = @PageNum -1 

SET @RowStart = @PageSize * @PageNum + 1; 
SET @RowEnd = @RowStart + @PageSize - 1 ; 

With Cust AS 
     ( SELECT *, 
       ROW_NUMBER() OVER (order by ID DESC) as RowNumber 
       FROM Sample WHERE sampleID = @sampleID )

select * 
from Cust 
Where RowNumber >= @RowStart and RowNumber <= @RowEnd end

and in my windows form where datagridview is I have

C#
page.Text = "1";
                size.Text = "25";

                SqlCommand command = new SqlCommand("sample_proc", cs);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter param = new SqlParameter("@PageNum", SqlDbType.Int);
                SqlParameter param1 = new SqlParameter("@PageSize", SqlDbType.Int);
                SqlParameter param2 = new SqlParameter("@sampleID", SqlDbType.VarChar, -1);

                param.Direction = ParameterDirection.InputOutput;
                param1.Direction = ParameterDirection.InputOutput;
                param2.Direction = ParameterDirection.InputOutput;

                param.Value = page.Text;
                param1.Value = size.Text;

                string[] sm1 = textBox1.Text.Split(',');

                foreach (string value in sm1)
                {                    
                    param2.Value = value;
                }
                
                command.Parameters.Add(param);
                command.Parameters.Add(param1);
                command.Parameters.Add(param2);

                daSample.SelectCommand = command;

                dsSample.Clear();
                daSample.Fill(dsSample, "Sample");

                SampleBS.DataSource = dsSample.Tables["Sample"];

                datagridview.DataSource = SampleBS;
but when I run it I get nothing, empty

anyone :)
Posted
Updated 19-Apr-13 9:40am
v2
Comments
ZurdoDev 19-Apr-13 15:23pm    
That's a lot to read through, but SQL does not have lists, so you send in a delimited list of ids as a varchar.

After that line:
C#
command.Parameters.Add(param2);

add:
C#
SqlDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable("Sample");
dt.Load(reader);
datagridview.DataSource = dt


Of course, you can use SelectCommand[^] and SelectParameters[^] too. ;)
 
Share this answer
 
 
Share this answer
 
Hello,

Heres how I handle a list of Ids as a parameter in SQL. First I have a function that converts a delimiter seperated string of Id's into a table.
SQL
CREATE FUNCTION fnGetNumericTableFromCommaSeparatedList
(
	@CommaSeparatedList VARCHAR(4000),
	@Delimiter CHAR
)
RETURNS @ListValues TABLE 
(
	Id BIGINT NULL
)
AS
BEGIN

DECLARE @CommaIndex INT
DECLARE @TextValue VARCHAR(255)

SET @CommaIndex = CHARINDEX(@Delimiter, @CommaSeparatedList)

WHILE @CommaIndex > 0
BEGIN					 
	SET @TextValue = LTRIM(RTRIM(SUBSTRING(@CommaSeparatedList, 1, @CommaIndex - 1)))
	
	IF(@TextValue != '' AND (ISNUMERIC(@TextValue) = 1))
	BEGIN			 
		INSERT @ListValues (Id) VALUES (CONVERT(BIGINT, @TextValue))
	END
	 
	IF(@TextValue = 'NULL')
	BEGIN
		INSERT @ListValues (Id) VALUES (NULL)
	END 
	
	SET @CommaSeparatedList = SUBSTRING(@CommaSeparatedList, @CommaIndex + 1, LEN(@CommaSeparatedList)) 
	 
	SET @CommaIndex = CHARINDEX(@Delimiter, @CommaSeparatedList)
END
	
IF (@CommaSeparatedList != '' AND (ISNUMERIC(@CommaSeparatedList) = 1))
BEGIN
	INSERT INTO @ListValues (Id) VALUES (CONVERT(BIGINT, @CommaSeparatedList))
END	
	
IF(@CommaSeparatedList = 'NULL')
BEGIN
	INSERT @ListValues (Id) VALUES (NULL)
END	

RETURN

END


Then I join to the function
SQL
DECLARE @ListOfIds VARCHAR(255)
SET @ListOfIds = '1, 3, 5, 6, 6'

SELECT		f.*
FROM		Foo f
INNER JOIN	dbo.fnGetNumericTableFromCommaSeparatedList(@ListOfIds, ',') l
   ON	        f.Id = l.Id


Hope that helps,
Matt
 
Share this answer
 
Quote:
@PageSize int output,
@PageNum int output,
@sampleID varchar(MAX) output
why are you using this.You are passing parameter as input not as output
 
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