• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

asp.net web forms C# , display stored procedure result on label

Hi experts,

I saw this example:

ex1.PNG
How to return Output parameter from Stored Procedure in ASP.Net using C# and VB.Net
https://www.aspsnippets.com/Articles/How-to-return-Output-parameter-from-Stored-Procedure-in-ASPNet-in-C-and-VBNet.aspx

In that example which he gives the sql server and asp.net code.

He has a stored procedures that looks like this:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetFruitName]
      @FruitId INT,
      @FruitName VARCHAR(30) OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
     
      SELECT @FruitName = FruitName
      FROM Fruits
      WHERE FruitId = @FruitId
END

Open in new window



How would I revise that exact same example.
If my stored procedure looked like this.
I just want to display the name of the fruit on the label of the asp.net page for whatever fruitid i submitted in the textbox.

USE [FruitsDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GetFruitNameTest1]
	@FruitId INT

AS
BEGIN
	SET NOCOUNT ON;
	
    SELECT [FruitId]
          ,[FruitName]
    FROM [FruitsDB].[dbo].[Fruits]
	WHERE FruitId = @FruitId
END

GO

Open in new window

0
maqskywalker
Asked:
maqskywalker
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Instead of cmd.ExecuteNonQuery();

SqlDataReader dr = new SqlDataReader();
dr = cmd.ExecuteReader();

if (dr.HasRows)
{
   if (dr.Read())
     lblFruitName.Text = "Fruit Name: " + dr["FruitName"].ToString();
}

Open in new window


You can also review this for reference:
http://www.dotnetcodecentral.com/Post/56/ado-net-querying-for-data/retrieve-a-row-using-sqldatareader-in-ado-net
0
 
maqskywalkerAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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