Varchar value not passing into SQL Server stored procedure

Prakash

Following is my code to call my stored procedure:

SqlCommand commandSql = new SqlCommand("GetbyProgramID")

commandSql.CommandType = CommandType.StoredProcedure;

//commandSql.Parameters.Add("@program_id", SqlDbType.VarChar, 5, programID);
commandSql.Parameters.Add("@program_id", System.Data.SqlDbType.VarChar, 5).Value = programID;

SqlParameter returnValue = new SqlParameter("@result", SqlDbType.Int);
returnValue.Direction = ParameterDirection.Output;
commandSql.Parameters.Add(returnValue);

DBAccessHelper.Execute(commandSql);

var result = returnValue.Value != System.DBNull.Value ? returnValue.Value.ToString() : string.Empty;
return result;

And here is my stored procedure:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetbyProgramID]
       @program_id  varchar,
       @result int OUTPUT 
AS 
BEGIN 
    SELECT @result = system_id 
    FROM dbo.agency_mapping 
    WHERE program_id = @program_id  

    RETURN
END

I always get an empty values as the result in my C# code.

Not sure what I am doing wrong.

The values I pass are correct.

Missing anything important ?

Steve

You have declared

ALTER PROCEDURE [dbo].[GetbyProgramID]
       @program_id  varchar,
       @result int OUTPUT 

this means that your stored procedure receives a parameter named @program_id with size of 1 char. Probably your SELECT fails for this reason.

Try instead to declare

ALTER PROCEDURE [dbo].[GetbyProgramID]
       @program_id  varchar(5),
       @result int OUTPUT 

as from your C# code.
Do not remove the size in the C# code because it is useful to let the Sql Server Engine prepare an optimization plan that can be reused if you call your query a second time.

Mode info here: How Data Access code Affects Database Performances

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to parse a VARCHAR passed to a stored procedure in SQL Server?

분류에서Dev

Executing Sql Server Stored Procedure and getting OUTPUT INSERTED value

분류에서Dev

Passing multiple value parameter to stored procedure

분류에서Dev

Converting Stored Procedure into a query (SQL Server Compact)?

분류에서Dev

SQL server create stored procedure syntax error

분류에서Dev

how to secure a valuable stored procedure in sql server

분류에서Dev

SQL stored procedure: how to concatenate parameter value?

분류에서Dev

Passing Strings to a Class Method that passes information to stored SQL procedure

분류에서Dev

SQL server stored procedure issue in via with c#

분류에서Dev

SQL Server : stored procedure many-to-many table query

분류에서Dev

How to pass schema as parameter to a stored procedure in sql server?

분류에서Dev

SQL Server: EXECUTE AS clause of stored procedure not granting sysadmin permissions

분류에서Dev

How can I back up a stored procedure in SQL Server?

분류에서Dev

SQL Server Stored procedure aborting when called from php

분류에서Dev

Nested stored procedure in SQL

분류에서Dev

how to split a xml format data into row column format in sql server 2008 using stored procedure

분류에서Dev

SQL Server 2008 R2 - Running a Stored Procedure against Linked Servers

분류에서Dev

SQL Server CLR Library Stored Procedure static object instantiated more than once

분류에서Dev

How I do know if SQL Server Stored Procedure that performs an Update worked?

분류에서Dev

Return result set from SQL Server stored procedure to vb.net

분류에서Dev

Emailing from SQL Server stored procedure with set-based logic, avoiding cursors

분류에서Dev

Passing data to a stored procedure that accepts Table Valued Parameter using pyodbc

분류에서Dev

C# - Passing parameter to stored procedure and return IEnumerable

분류에서Dev

C# - Passing parameter to stored procedure and return IEnumerable

분류에서Dev

Call stored procedure from PL/SQL Job

분류에서Dev

SQL Stored Procedure Get Distinct and Update

분류에서Dev

PL/SQL Stored Procedure - IF THEN ELSE condition

분류에서Dev

SQL Stored procedure does not enter IF and CASE condition

분류에서Dev

SQL Server의 DATALENGTH VARCHAR

Related 관련 기사

  1. 1

    How to parse a VARCHAR passed to a stored procedure in SQL Server?

  2. 2

    Executing Sql Server Stored Procedure and getting OUTPUT INSERTED value

  3. 3

    Passing multiple value parameter to stored procedure

  4. 4

    Converting Stored Procedure into a query (SQL Server Compact)?

  5. 5

    SQL server create stored procedure syntax error

  6. 6

    how to secure a valuable stored procedure in sql server

  7. 7

    SQL stored procedure: how to concatenate parameter value?

  8. 8

    Passing Strings to a Class Method that passes information to stored SQL procedure

  9. 9

    SQL server stored procedure issue in via with c#

  10. 10

    SQL Server : stored procedure many-to-many table query

  11. 11

    How to pass schema as parameter to a stored procedure in sql server?

  12. 12

    SQL Server: EXECUTE AS clause of stored procedure not granting sysadmin permissions

  13. 13

    How can I back up a stored procedure in SQL Server?

  14. 14

    SQL Server Stored procedure aborting when called from php

  15. 15

    Nested stored procedure in SQL

  16. 16

    how to split a xml format data into row column format in sql server 2008 using stored procedure

  17. 17

    SQL Server 2008 R2 - Running a Stored Procedure against Linked Servers

  18. 18

    SQL Server CLR Library Stored Procedure static object instantiated more than once

  19. 19

    How I do know if SQL Server Stored Procedure that performs an Update worked?

  20. 20

    Return result set from SQL Server stored procedure to vb.net

  21. 21

    Emailing from SQL Server stored procedure with set-based logic, avoiding cursors

  22. 22

    Passing data to a stored procedure that accepts Table Valued Parameter using pyodbc

  23. 23

    C# - Passing parameter to stored procedure and return IEnumerable

  24. 24

    C# - Passing parameter to stored procedure and return IEnumerable

  25. 25

    Call stored procedure from PL/SQL Job

  26. 26

    SQL Stored Procedure Get Distinct and Update

  27. 27

    PL/SQL Stored Procedure - IF THEN ELSE condition

  28. 28

    SQL Stored procedure does not enter IF and CASE condition

  29. 29

    SQL Server의 DATALENGTH VARCHAR

뜨겁다태그

보관