3

I am having trouble returning data from my sql-server database to an aspx page using a stored procedure and was hoping somebody could highlight where I am going wrong.

When I run the project data is successfully input into the table but nothing is returned on the following page (Confirm.aspx)

Confirm.aspx.cs

using Devworks;

namespace OSQARv0._1
{
    public partial class Confirm_Questionnaire : System.Web.UI.Page
    {
        OscarSQL b;

        protected void Page_Load(object sender, EventArgs e)
        {
            b = new OscarSQL();
            string qstname = b.GetQuestionName();
            ReturnQstID.Text = qstname;          

        }// End Page_Load

    } // Emd Class Confirm_Questionnaire

} // End Namespace

SQL.cs (App Code)

public OscarSQL()
        {
            _productConn = new SqlConnection();
            _productConnectionString += "data source=mssql.database.co.uk; Initial Catalog=devworks_oscar;User ID=username;Password=password";
            _productConn.ConnectionString = _productConnectionString;
        }
    public string GetQuestionName()
            {
                SqlCommand myCommand = new SqlCommand("GetQuestion", _productConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter retval = myCommand.Parameters.Add("@QUESTTEXT", SqlDbType.VarChar);
                retval.Direction = ParameterDirection.Output;
                _productConn.Open();
                string returnvalue = (string)myCommand.Parameters["@QUESTTEXT"].Value;
                _productConn.Close();
                return returnvalue;
            }

Stored Procedure

USE [devworks_oscar]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [hgomez].[GetQuestion]  

AS
    /*SET NOCOUNT ON;*/
    SELECT QuestionText FROM [Questions] WHERE QuestionnaireID = 21
    RETURN

Any help would be much appreciated.

1
  • with dapper: string value = connection.Query<string>("GetQuestion", commandType: CommandType.StoredProcedure).Single(); - just sayin' Commented Nov 9, 2011 at 19:37

4 Answers 4

8

You aren't executing the proc. You are setting it up, but not returning it.

string returnvalue = (string)myCommand.ExecuteScalar();
Sign up to request clarification or add additional context in comments.

6 Comments

@MarcGravell looks like a scalar to me? SELECT QuestionText FROM [Questions] WHERE QuestionnaireID = 21
my apologies - I read it in a hurry and imagined a SELECT @somevar = ...
@MarcGravell I will in future be using a variable such as @personID, will ExecuteScalar() still be correct?
@Rupert, if you are assigning a parameter in a SQL statement (which is what he thought), then ExecuteScalar won't return anything, but if you are just selecting from an existing variable, then you still want ExecuteScalar
@Rupert, you should use ExecuteScalar, because the result of that query returns precisely one value.
|
4

Your stored procedure does not use an output parameter to return a value, so instead, use ExecuteScalar():

string returnvalue = (string)myCommand.ExecuteScalar();

2 Comments

In this particular case he should use ExecuteScalar
@stefan, yeah, realized that immediately after I posted :)
1

A return value is what the stored procedure returns, typically this is an integer value, sometimes it is used to drive business logic or error conditions. In your case, you want the data that is returned, not the return value. So, use ExecuteScalar if the query is returning a single value or ExecuteReader if the query is returning one or more records of data. ExecuteNonQuery is typically used for insert/updates and/or delete operations.

Comments

1
ExecuteScalar() is a good idea if you know there is only one occurance of that record in the database, ExecuteScalar if I am not mistaken only returns the first record or occurance it finds.. you could use a datareader and do datareader.ExecuteReader() this is forward only but there are ways to make it biDirectional hehe... :) anyway if you want to loop thru the results use ExecuteReader() if you want to execute a StoredProceduere with UDATE,DELETE, or INSERT use reader.ExecuteNonQuery().

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.