By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,808 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:labelso 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...
Aug 27 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You simply put the label in a repeater, since ExecuteScaler simply returns
the first field in the first record of the result rowset.

"Homer J. Simpson" <ro**@127.0.0.1wrote in message
news:ue*************@TK2MSFTNGP06.phx.gbl...
>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:labelso 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...

Aug 27 '07 #2

P: n/a
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?
Aug 27 '07 #3

P: n/a
...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".
Aug 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.