Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored Proc in sql that does not return the value

My function isn't returning anything - strReturn is empty:

        try
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
                new SqlParameter("@MerchantID", MercahntID),
                new SqlParameter("@LoactionID", LoactionID)
            };

            SqlHelper.ExecuteNonQuery(DbConnString, System.Data.CommandType.StoredProcedure, "GetMerchantLocationZip", parameter);

            return strReturn;
        }
        catch (Exception ex)
        {
            LogError("Error Occurred When Retrieving Mercahnt Location Zip: MercahntID:" + MercahntID.ToString(), ex);
            return strReturn;
        }
    }

When I execute this stored proc using 'exec GetMerchantLocationZip (3333, 373773)' I get the correct zipcode in SQL. Why don't I get it in Visual Studio?

Create PROCEDURE [dbo].[GetMerchantLocationZip](
@MerchantID bigint,
@LoactionID bigint)

AS 

Begin

Select Zip FROM Merchant_Location 
where MerchantID=@MerchantID AND LocationID =@LoactionID

End

I am learning, so apologies if it's a obvious error. Thanks all!

like image 993
challengeAccepted Avatar asked Jul 15 '26 00:07

challengeAccepted


1 Answers

You're not getting results because you're not executing the code as a Query.

You're calling SqlHelper.ExecuteNonQuery() which doesn't return any results.

It looks like you're using the SqlHelper application block, so I think the code you want would be (if you're returning multiple rows in the query):

DataSet ds = SqlHelper.ExecuteDataSet(DbConnString,
                                      CommandType.StoredProcedure,
                                      "GetMerchantLocationZip",
                                      parameter);

ds will then contain the results of the query.

If you're trying to retrieve a single value from the database rather than a set of rows, then your code would be:

object zip = SqlHelper.ExecuteScalar(DbConnString,
                                     CommandType.StoredProcedure,
                                     "GetMerchantLocationZip",
                                     parameter);
like image 104
Justin Niessner Avatar answered Jul 17 '26 15:07

Justin Niessner



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!