Click here to Skip to main content
15,886,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have the following problem. I want to access some SQL output parameter
via my c# code.

The procedure looks like:
SQL
CREATE PROCEDURE sp_DoSome

@VALUE int output,@OTHER nvarchar(MAX) output

AS
BEGIN
	
SELECT @VALUE =  -198;
SELECT @OTHER = 'Works';

END
GO


And the code to access the values:
C#
 using (SqlCommand com = new SqlCommand 
{ CommandType = CommandType.StoredProcedure, 
  CommandText = "sp_AskForUpdate", 
  Connection = _connection })
                 
    {
      com.Parameters.AddWithValue("@VALUE", SqlDbType.Int);
      com.Parameters.AddWithValue("@OTHER", SqlDbType.NVarChar);
      com.Parameters["@VALUE"].Direction = ParameterDirection.Output;
      com.Parameters["@OTHER"].Direction = ParameterDirection.Output;
      _connection.Open();
      com.ExecuteNonQuery();
      temp = com.Parameters["@VALUE"].Value.ToString() + com.Parameters["@OTHER"].Value;
      _connection.Close();
                }

....goes on


Doing this just with int's works fine. But in all other cases generates an exception
like this (nvarchar):
C#
04/11/2013 11:10:00 Error converting data type nvarchar(max) to int.Void OnError(System.Data.SqlClient.SqlException, Boolean, System.Action`1[System.Action])System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar(max) to int.
   bei System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   bei System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   bei System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   bei System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   bei System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
   bei System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   bei System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   bei System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   bei JuloGroupService.Service1.AskForUpdate(Int32 companyId, Int32 miaId, Boolean& free) in C:\Users\schafmann\Desktop\visual studio 2010\Projects\JuloGroupService\JuloGroupService\Service1.svc.cs:Zeile 331.
ClientConnectionId:d955c0e9-079a-452c-8538-005e91776d3a


I dont understand why this generates a converting error???

Thanks for any help
Andree
Posted
Updated 11-Apr-13 0:58am
v2
Comments
CHill60 11-Apr-13 5:39am    
How is temp declared?
Schafmann 11-Apr-13 5:43am    
temp is declared as string
CHill60 11-Apr-13 5:58am    
You could try adding .ToString() to the + com.Parameters["@OTHER"].Value; bit. Doesn't help to explain why it's happening I know! (But it might make the problem "go away")
Schafmann 11-Apr-13 6:20am    
thanks, but this aslo dont work. I have changed the type for the @Other
Parameter to "SqlDbType.Int" and it works. But any other type wont work (the
same error message like above)
tumbledDown2earth 11-Apr-13 6:09am    
You should consider the return values from the sp. They should match

1 solution

For now i have solved the Problem in this way:

C#
 using (SqlCommand com = new SqlCommand 
{ CommandType = CommandType.StoredProcedure, 
  CommandText = "sp_AskForUpdate", 
  Connection = _connection })
                 
    {
      com.Parameters.AddWithValue("@VALUE", SqlDbType.Int);
      com.Parameters.AddWithValue("@OTHER", SqlDbType.NVarChar);
      com.Parameters["@VALUE"].Direction = ParameterDirection.Output;
      com.Parameters["@OTHER"].Direction = ParameterDirection.Output;

      //Adding this to my code has solved the problem
      com.Parameters["@OTHER"].Size = 255;
      com.Parameters["@OTHER"].Value = " ";

      _connection.Open();
      com.ExecuteNonQuery();
      temp = com.Parameters["@VALUE"].Value.ToString() + com.Parameters["@OTHER"].Value;
      _connection.Close();
                }
 
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