views:

1095

answers:

2

I've got a sproc and some C# calling it. My problem is that both the (N)VARCHAR values only have the first character returned. Everything else is fine. Why would only the 1st character be returned?

So

Content = (string)cmd.Parameters["@SmsContent"].Value,
ToNumber = (string) cmd.Parameters["@ToNumber"].Value,

both only get the 1st character returned.

Here's my sproc:

ALTER PROCEDURE [dbo].[GetNextSms]
(   
    @SmsId UNIQUEIDENTIFIER OUTPUT, 
    @SmsContent NVARCHAR OUTPUT, 
    @ToNumber VARCHAR OUTPUT,
    @TimeAccepted DATETIME OUTPUT
)
AS
BEGIN TRANSACTION
    -- 1. Get 1 row
    SET ROWCOUNT 1
        SELECT @SmsId = SmsId FROM SendQueue
        WHERE ProcessingStarted = 0
    SET ROWCOUNT 0
    -- 2. Set as processing
    UPDATE SendQueue 
        SET ProcessingStarted = 1
        WHERE SmsId = @SmsId
    -- 3. Return data
    SELECT @SmsId = SmsId,
        @SmsContent = SmsContent,
        @ToNumber = ToNumber,
        @TimeAccepted = TimeAccepted
    FROM SendQueue
    WHERE SmsId = @SmsId;

COMMIT

And here is my C#

connection.Open();
var cmd = new SqlCommand(@"GetNextSms", connection) {CommandType = CommandType.StoredProcedure};
SqlParameter param = cmd.Parameters.Add("@SmsId", SqlDbType.UniqueIdentifier);
param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@ToNumber", SqlDbType.VarChar,50);
param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@SmsContent", SqlDbType.NVarChar,1024);
param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@TimeAccepted", SqlDbType.DateTime);
param.Direction = ParameterDirection.Output;


if (cmd.ExecuteNonQuery() > 0)
{
    sms = new Sms
              {
                  SmsId = ((Guid) cmd.Parameters["@SmsId"].Value),
                  Content = (string)cmd.Parameters["@SmsContent"].Value,
                  ToNumber = (string) cmd.Parameters["@ToNumber"].Value,
                  TimeAccepted = ((DateTime) cmd.Parameters["@TimeAccepted"].Value)
              };
}
+5  A: 

Base on a bit of searching (i.e. I haven't tried it) you should declare your stored procedure parameters with a length too, i.e.

ALTER PROCEDURE [dbo].[GetNextSms]
(   
    @SmsId UNIQUEIDENTIFIER OUTPUT, 
    @SmsContent NVARCHAR(1024) OUTPUT, 
    @ToNumber VARCHAR(50) OUTPUT,
    @TimeAccepted DATETIME OUTPUT
)

Apologies if that's not the answer, but it's worth a try. It seems to agree with the MSDN doc that states that the default length for NVARCHAR/VARCHAR is 1.

Jon Skeet
A: 

That fixed it. Thanks.

Mr. Flibble