VB.NET and return value from stored procedure

Hello,

Below is the my stored procedure. When I use .ExecuteScalar from VB.NET, I expect the return value from the SP as 0 or 1. But I always get blank i.e. "". Why?


CREATE procedure [dbo].[sp_VoidTransaction_Repost](@intTransactionNr as Integer, @Sales_date as Datetime, @intAccountNr as Integer)
AS

Declare @strSQL as varchar(8000)
Declare @intNewTrnNr as Integer

BEGIN TRANSACTION
-- Insert voided record into Sales Master
Insert Into Sales_Master( ShipNr, Sales_TrnNr, Partial_Sales_Nr,
      ServerID, Table_Nr, Account_Nr, Routing_Account, Transaction_Status, Cabin_Nr, Sales_date,
      Cruise_Nr, Outlet_Nr, Stock_Code, NetSalesPrice, GrossSalesPrice, Gratuity, Discount,
      IsTaxApplicable, TaxType, TaxAmount, Amount_due, IsSalesDeducted_FromStock, Settlement_type,
      Payment_Type, IsSalePartiallyClosed, NewServerID, Extra_Gratuities, isSelected, AIAccount_Nr,
      AllInclusiveDiscount, IsAllInclusive, VoidFlag )
             Select ShipNr, Sales_TrnNr, Partial_Sales_Nr,
      ServerID, Table_Nr, Account_Nr, Routing_Account, Transaction_Status, Cabin_Nr, @Sales_date,
      Cruise_Nr, Outlet_Nr, Stock_Code, (-1) * NetSalesPrice, (-1) * GrossSalesPrice, (-1) * Gratuity,
      (-1) * Discount, IsTaxApplicable, TaxType, (-1) * TaxAmount, (-1) * Amount_due, IsSalesDeducted_FromStock,
      Settlement_type, Payment_Type, IsSalePartiallyClosed, NewServerID, (-1) * Extra_Gratuities,
      isSelected, AIAccount_Nr, (-1) * AllInclusiveDiscount, IsAllInclusive, 1
      From Sales_Master Where Sales_Master.Transaction_Nr = 507627


-- On error return False
IF @@Error <> 0
      BEGIN
            ROLLBACK TRANSACTION
            Return 0
      END

-- Select Transaction_Nr of inserted record
Select @intNewTrnNr = Max(Transaction_Nr) From Sales_Master

-- On error return False
IF @@Error <> 0
      BEGIN
            ROLLBACK TRANSACTION
            Return 0
      END

-- Insert into Sales Details
DECLARE  @ShipNr As varchar(15)
DECLARE  @Batch As smallint
DECLARE  @PLU_ID As Int
DECLARE  @SortID As smallint
DECLARE  @Sales_Qty As smallint
DECLARE  @Unit_Price As Numeric(15,2)
DECLARE  @PLU_SpecifierID As Int
DECLARE  @PLU_SpecifierSurcharge As Numeric (15,2)
DECLARE  @TaxPercent As Numeric (15,2)
DECLARE  @Discount As Numeric (15,2)

DECLARE SALES_CURSOR CURSOR FOR
      SELECT ShipNr, Batch, PLU_ID, SortID, Sales_Qty, Unit_Price, PLU_SpecifierID, PLU_SpecifierSurcharge, TaxPercent, Discount From Sales_Details Where Transaction_Nr = @intTransactionNr

OPEN SALES_CURSOR
      FETCH NEXT FROM SALES_CURSOR INTO  @ShipNr,  @Batch,  @PLU_ID,  @SortID,  @Sales_Qty,  @Unit_Price,  @PLU_SpecifierID,  @PLU_SpecifierSurcharge,  @TaxPercent,  @Discount

      WHILE @@fetch_status=0
      
          BEGIN
            INSERT INTO Sales_Details Values(@ShipNr, @intNewTrnNr, @Batch,  @PLU_ID,  @SortID,  (-1) * @Sales_Qty,  @Unit_Price,  @PLU_SpecifierID,  @PLU_SpecifierSurcharge,  @TaxPercent,  (-1) * @Discount)

              FETCH NEXT FROM SALES_CURSOR INTO @ShipNr,  @Batch,  @PLU_ID,  @SortID,  @Sales_Qty,  @Unit_Price,  @PLU_SpecifierID,  @PLU_SpecifierSurcharge,  @TaxPercent,  @Discount
          END

CLOSE SALES_CURSOR

DEALLOCATE SALES_CURSOR

-- On error return False
IF @@Error <> 0
      BEGIN
            ROLLBACK TRANSACTION
            Return 0
      END
Else
      Begin

            COMMIT TRANSACTION

            RETURN 1
      End
GO
vj_miAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FocusynCommented:
On ExecuteScalar() with stored procedures, most SQL Server-side errors will not throw an exception to your .NET assembly.  It will simply return no value.  Try putting your Exec command in a try catch block and see if it throws an exception, but my guess is something on the SQL side is causing that.  

On your code side, also make sure you have explicitly declared COmmandType.StoredProcedure
and added all the sp variables as yourCommand.Parameter.Add etc rather than trying to concatenate a long commandtext string.
FocusynCommented:
Oh and also if you have the access and the know-how, use some SQL auditing and check for errors on that side.
cubixSoftwareCommented:
Within your code where you have defined Parameters.Add for (intTransactionNr etc.) you should define another as follows


myCommand.Parameters.Add("@myReturn")
myCommand.Parameters("@myReturn").Direction = ParameterDirection.ReturnValue

or you could simply declare an OUTPUT parameter for your stored procedure and set its value to 0 or 1 within the sp and then define as follows

myCommand.Parameters.Add("@myReturn")
myCommand.Parameters("@myReturn").Direction = ParameterDirection.Output

myCommand.ExecuteNonQuery()
If myCommandParameters("@myReturn").Value = 0 then
    ' do something
ELSE
    ' do something else
End If


HTH

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TakeoutdinnerCommented:
Hi there,

I use QueriesTableAdapter. So your advice will not work with mine.
Would you please advise me for my current QueriesTableAdapter?

Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.