[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4824
  • Last Modified:

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
0
vj_mi
Asked:
vj_mi
  • 2
1 Solution
 
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.
0
 
FocusynCommented:
Oh and also if you have the access and the know-how, use some SQL auditing and check for errors on that side.
0
 
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
0
 
TakeoutdinnerCommented:
Hi there,

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

Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now