Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I want to pass values to IN keyword in sql query from front end .when i give values directly in query its getting. but pass values from front end its not getting. getting error message
Conversion failed when converting the varchar value '2,3' to data type int.
C#
CREATE TABLE TestIn (name VARCHAR(15), id int identity)
insert into TestIn(name) values('Ammu'),('unni'),('geethu'),('tintu'),('joby'),('jinu')
declare @id varchar(300)
set @id='2,3'
SELECT * FROM TestIn  where id in (@id)

DROP TABLE TestIn

How to do this.Thanks in Advance,

Amritha
Posted
Updated 20-Aug-13 21:01pm
v3

It's not that simple...:laugh:
Have a look here: Using comma separated value parameter strings in SQL IN clauses[^]
 
Share this answer
 
Comments
Maciej Los 21-Aug-13 3:07am    
You are too fast for me ;)
I've been forced to delete my answer with the link to your tip...
OriginalGriff 21-Aug-13 3:42am    
:laugh:
I can get at my articles faster than you, probably - they're on a drop down in the TRHC of the page.
amritha444 21-Aug-13 3:47am    
thanks OriginalGriff
its working well
OriginalGriff 21-Aug-13 3:49am    
You're welcome!
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (SqlConnection sqlCon = new SqlConnection(@"Data Source=127.0.0.1; Database=DatabaseName; User id=uid; Password=pwd; Integrated security=true;"))
        {
            sqlCon.Open();

            SqlCommand sqlCmd = new SqlCommand("usp_data", sqlCon);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            
            sqlCmd.Parameters.Add(new SqlParameter("@vals", "2,3,4,5"));
            //OR
            sqlCmd.Parameters.Add(new SqlParameter("@vals", "A,B,C,D"));

            int recAffected = sqlCmd.ExecuteNonQuery();

            sqlCon.Close();
        }
    }
}




SQL
use [DatabaseName]
go

create procedure [dbo].[usp_data]
	@vals varchar(max)=null
as	
BEGIN
	declare @str varchar(max)
	declare @vals varchar(max)
	
	-- for numeric values
	set @str = 'select * from tbl_data where id in(' + @vals + ')'
	print @str
	exec (@str)
	
	-- for text values
	set @vals = replace(@vals, ',', char(39)+','+char(39))
	set @str = 'select * from tbl_data where id in(' + char(39) + @vals + char(39) + ')'
	print @str
	exec (@str)
END	


DineshT : 91+9829104666
 
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