Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
cmd = new SqlCommand("SELECT PrivillegesName, PrivillegesGroup from tblPrivilleges where PrivillegesID in ( " + privillege + " ) ", cn);
            SqlDataReader dr = cmd.ExecuteReader();


i want to write store procedure for this...my problem is that how to pass ( " + privillege + " ) as @ parameter
Posted
Updated 5-May-10 1:04am
v2

First, the idea of a parameter is completely out of context here. You're passing a query string to your reader, so you have to build it BEFORE passing it to your reader.

Second, checking to see if a value is in a set requires that you specify the set. In your case, it means formatting a string in such a way as to allow the sql to execute, such as "1,2,3,4".

Lastly, you misspelled "privilege"...
 
Share this answer
 
You add a parameter to the SqlCommand object like so;
cmd.Parameters.AddWithValue("@privillege", privillege);


also don't forget to change the SqlCommand.CommandType to stored procedure e.g.
cmd.CommandType = CommandType.StoredProcedure;


However in your case with the query as specified you can't use parameters as you are using IN as part of the WHERE clause you can't pass parameters for use in this case.
 
Share this answer
 
v2
pass "privillege" as varchar parameter and in your procedure use sp_executesql to execute statement




SQL
create proceudre getitems 
@privilleg varchar(max)
as 
begin


SET @SQLString = "SELECT *
    FROM table1
    WHERE  items in (@item)";


DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);

SET @ParmDefinition = "@item varchar(max)";


EXECUTE sp_executesql
    @SQLString
    ,@ParmDefinition
    ,@item = @privilleg

end
 
Share this answer
 
u can pass this way the parameters to proc

SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("SP_SAVE_EMP", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EMPCODE", code);
cmd.Parameters.AddWithValue("@NAME",name);
cmd.Parameters.AddWithValue("@AGE", age);
cmd.Parameters.AddWithValue("@DOB", name);
cmd.Parameters.AddWithValue("@EMAIL", email);
cmd.Parameters.AddWithValue("@IMAGE", image);
con.Open();
status = (int)cmd.ExecuteNonQuery();
 
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