How to pass parameters to stored procedure?

Posted by Anilkumar431 under Sql Server on 1/8/2013 | Points: 10 | Views : 1699 | Status : [Member] | Replies : 3
hi all,
i have this stored procedure
i need to pass parameters to this procedure and how it should be?
create procedure insert_record(@e int,@n nvarchar(50),@s smallmoney)
as
begin
begin try
if exists(select * from emp_temp where eno=@e)
raiserror('Employee is already exists',15,1)
if @s<3000
raiserror('sal>3000',15,1)
insert into emp_temp values(@e,@n,@s)
end try
begin catch
print error_message()
end catch
end




Responses

Posted by: Neer on: 1/8/2013 [Member] Starter | Points: 25

Up
0
Down
SqlParameter[] param1 = new SqlParameter[]{
new SqlParameter("@e",ValueE),
new SqlParameter("@n",ValueN),
new SqlParameter("@S",ValueS)
};
Boolean result = s.ExecuteSP ("insert_record", param1);

s is object of your code file
ExecuteSP is method of your code file to execute non Query.

-Neer

Anilkumar431, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubh on: 1/12/2013 [Member] Starter | Points: 25

Up
0
Down
Hii Friend

connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionInfo"]); 

sql = "aStoredProc";
command = new SqlCommand(sql, connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameter.Add(new SqlParameter("@FirstName", SqlDbType.VarChar)).Value = sFirstname;
command.Parameter.Add(new SqlParameter("@SurName", SqlDbType.VarChar)).Value = sSurname;
command.Parameter.Add(new SqlParameter("@SurName", SqlDbType.VarChar)).Value = sSurname;
SqlParameter ParamId = cmd.Parameters.Add( "@Id", SqlDbType.Int);
command.Direction = ParameterDirection.InputOutput;
command.Parameter.Add(ParamId);
connection.Open();
command.ExecuteNonQuery();
int ID = ParamId.Value;
connection.Close();


enjoy with this....

Shubham Agrahari

Anilkumar431, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/15/2013 [Member] Starter | Points: 25

Up
0
Down
hello anil kumar,
u can do as follows

  SqlCommand cmd = new SqlCommand("signin", db.sqlcon);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.AddWithValue("@password", SqlDbType.Int).Value = TextBox2.Text;
SqlDataReader dr = cmd.ExecuteReader ();


njoy


Anilkumar431, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response