2

I am trying to get/return multiple values from an SQL-Server database using and display them on an ASP.NET page.

I am using a stored procedure to perform the SELECT command on the Database side.

I am able to return the first value that matches the variable @PERSON but only one row is returned each time.

Any help would be much appreciated.

Database handler class

public MainSQL()
{
       _productConn = new SqlConnection();
       _productConnectionString += "data source=mssql.database.co.uk;InitialCatalog=test_data;User ID=username;Password=password";
       _productConn.ConnectionString = _productConnectionString;
}
public string GetItemName(int PersonID)
{
     string returnvalue = string.Empty;
     SqlCommand myCommand = new SqlCommand("GetItem", _productConn);
     myCommand.CommandType = CommandType.StoredProcedure;
     myCommand.Parameters.Add(new SqlParameter("@PERSON", SqlDbType.Int));
     myCommand.Parameters[0].Value = PersonID;
     _productConn.Open();
     returnvalue = (string)myCommand.ExecuteScalar();             
     _productConn.Close();
     return (string)returnvalue;
 }

Stored Procedure

USE [test_data]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [ppir].[GetItem]    
    (
    @PERSON int
    )   
AS
    /*SET NOCOUNT ON;*/
    SELECT Description FROM [Items] WHERE PersonID = @PERSON
    RETURN

return.aspx

namespace test
{
    public partial class Final_Page : System.Web.UI.Page
    {
        MainSQL GetInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            int PersonId = (int)Session["PersonID"];
            GetInfo = new MainSQL();
            string itemname = GetInfo.GetItemName(PersonId);
            ReturnItemName.Text = itemname;

        } // End Page_Load

    } // End Class 

} // End Namespace

4 Answers 4

3

you should use sql datareader instead.:

ExecuteScalar returns you only the first result while reader returns you each result by loop until reader.Read()==false.

e.g. :

 DataReader data_reader= MySqlCommand.ExecuteReader( ); 
   while(data_reader.Read())  
  {
       ...
  }         
Sign up to request clarification or add additional context in comments.

Comments

2

I Change your GetItem method like this :

public List<string> GetItemName(int PersonID)
{
   List<string> returnvalues = new List<string>();
   SqlCommand myCommand = new SqlCommand("GetItem", _productConn);
   myCommand.CommandType = CommandType.StoredProcedure;
   myCommand.Parameters.Add(new SqlParameter("@PERSON", SqlDbType.Int));
   myCommand.Parameters[0].Value = PersonID;

   _productConn.Open();
   DataReader dr = myCommand.ExecuteReader(); 
   While(dr.Read() )  
   {
       returnvalues.Add(dr[0].ToString()); 
   }         

   _productConn.Close();
   return returnvalues;
 }

Comments

0

Does your stored procedure return one row for the id or does it return multiple rows? Ultimately you will need to loop over your results. If the stored procedure returns one record per call, then you need to loop over the ids in the aspx page. If the stored procedure returns multiple rows, then you can use and sqlDataReader instead of the ExecuteScalar call. Loop over the rows that were return and add them to some sort of a collection or list. Then return it to your page. You will still have to modify your aspx page to handle the collection, however.

Comments

0

You could use a SQLDataReader, or a SQLDataAdapter to fill in a DataSet, although for webforms you might be better served by separating your data access from your page entirely by way of using an ObjectDataSource.

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.