Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sproc Parameter Returning First Character Only

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)
              };
}
like image 543
Mr. Flibble Avatar asked Jul 19 '26 06:07

Mr. Flibble


1 Answers

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.

like image 194
Jon Skeet Avatar answered Jul 21 '26 18:07

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!