Stupid question - binding an <asp:label> --> stored procedure returning a scalar?

  • Thread starter Thread starter Homer J. Simpson
  • Start date Start date
H

Homer J. Simpson

I have the following stored procedure:

ALTER PROCEDURE [dbo].[spGetQuickNoteCount]
AS
BEGIN
SET NOCOUNT ON;
SELECT COUNT(*) FROM QUICKNOTES
END

....and the following data source in my .aspx file:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:csToolbar %>"
SelectCommand="spGetQuickNoteCount" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

Now...how do I go about binding an <asp:label> so it simply displays the
scalar returned by the stored procedure? I've used gridviews and repeaters
in the past, but those were always bound to a data source that returned
recordsets, not just a scalar.

I suppose I could write code in Page_Load() to connect to the database, run
the stored procedure and set the label's .Text property to the value
returned, but that's just adding extra code I'd like to avoid.

I'm sure I've missed something obvious here...
 
You simply put the label in a repeater, since ExecuteScaler simply returns
the first field in the first record of the result rowset.
 
You simply put the label in a repeater, since ExecuteScaler simply returns
the first field in the first record of the result rowset.

That's what I thought...but what do you use for the binding expression? I
tried the following:

<asp:Repeater ID="rptNoteCount" runat="server"
DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:Label ID="lblNotes" runat="server"><%#Eval( ??? ) %></asp:Label>
</ItemTemplate>
</asp:Repeater>

....what are you supposed to use for "???", since the field is unnamed?
 
...what are you supposed to use for "???", since the field is unnamed?

Never mind. I've named the field by modifying the stored procedure as:

SELECT COUNT(*) AS NUMBER FROM QUICKNOTES

....and then using Eval( "NUMBER" )


I could swear I had tried it already but it still came back blank. I'm as
much of a noob at SQL as I am at ASP.NET. :o)

Thanks "Sherif".
 
Back
Top