Passing a parameter to an sql stored procedure in c#
If you want to call a stored procedure using a SqlCommand, do not execute EXEC GetIslemIdleri
, execute just GetIslemIdleri
, setting CommandType to CommandType.StoredProcedure
:
cmd = new SqlCommand("GetIslemIdleri", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@CARIID", 110));
using (var reader = cmd.ExecuteReader()) //error occurs here
{
while (reader.Read())
{
islemidleri.Add(reader.GetInt32(0));
}
}
you forgot to add the prodecure`s name:
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetIslemIdleri";
cmd.Parameters.Add("@CARIID", SqlDBType.Int).Value = 110;
And do procedure as:
CREATE PROCEDURE [dbo].[GetIslemIdleri]
(
@CARIID int
)
AS
BEGIN
SET NOCOUNT ON;
SELECT ID FROM TBLP1ISLEM WHERE TBLP1ISLEM.CARI_ID = @CARIID
END
This has to work.
You need to make sure your SqlCommand is set to CommandType.StoredProcedure.
cmd.CommandType = CommandType.StoredProcedure