I have a clr stored procedure which has an output value I am calling the sp from a vb.net app with the code below. I am getting an error that says 'Error converting data type nvarchar to int. 1125092|63688|Ok' the values are exactly what I am expecting back from the sp but I can't understand why I am getting a convertion error can anyone help with this.
Code Calling the sp:
Try
Dim CN As New SqlConnection
CN.ConnectionString = My.Settings.myConnection
CN.Open()
Dim Profile As String = "40T"
Dim StartPosition As String = "53.6924582,-2.8730636000"
Dim Destination As String = "46.18186,1.380222"
Dim RetValue As String = "Testvalue"
Dim test As String ="53.816408201,-2.990582799997"
Dim cmd As SqlCommand = New SqlCommand("GetDistanceAndTime", CN)
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "GetDistanceAndTime"
cmd.Parameters.AddWithValue("@Profile", Profile )
cmd.Parameters.AddWithValue("@StartPosition", StartPosition)
cmd.Parameters.AddWithValue("@Destination", Destination)
cmd.Parameters.AddWithValue("@Result", SqlDbType.NVarChar )
cmd.Parameters("@Result").Direction = ParameterDirection.Output
Try
cmd.ExecuteNonQuery()
RetValue = cmd.Parameters ("@Result").value
If RetValue .Length > 2 Then
End If
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
The clr sp code:
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void SqlStoredProcedure1(SqlString Profile, SqlString StartPosition, SqlString Destination, ref SqlString Result)
{
SqlString result;
try
{
SqlPipe sqlPipe = SqlContext.Pipe;
result = DistanceRequest.InvokeService(Profile.ToString(), StartPosition.ToString(), Destination.ToString());
Result = result.ToString();
SqlContext.Pipe.Send(result.ToString());
}
catch (Exception ex)
{
result = ex.Message;
SqlContext.Pipe.Send(result.ToString());
}
}
}
Oddly when I execute the stored procedure from sql server manager it compiles the return value as an int! I am even more baffled.
USE [myDB] GO
DECLARE @return_value int, @Result nvarchar(50)
EXEC @return_value = [dbo].[GetDistanceAndTime]
@Profile = N'40T',
@StartPosition = N'53.6924582,-2.8730636000',
@Destination = N'46.18186,1.380222',
@Result = @Result OUTPUT
SELECT @Result as N'@Result'
SELECT 'Return Value' = @return_value
GO