Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SqlDataSource, Eval, and Label Text

22 views
Skip to first unread message

Jeffrey Walton

unread,
Nov 29, 2009, 7:01:01 AM11/29/09
to
Hi All,

In my body, I have similar to below. The query is good when I use it with
(for example) a DataView.

When I try to use the SqlDataSource as a standalone component, the label is
not populated with text from the result of Eval(). How does one retrieve the
fields? Or must the SqlDataSource only be used with DataViews, GridViews, etc?

Thanks,
Jeff

...
<asp:Label ID="Label" runat="server" Text='<%# Eval("Description") %>' />

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:ProductTableConnection %>"
SelectCommand="SELECT [ShortName], [Description] FROM [Products] WHERE
([ShortName] = @ShortName)">
<SelectParameters>
<asp:Parameter DefaultValue="XXXXX" Name="ShortName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Vijay

unread,
Nov 29, 2009, 9:44:50 AM11/29/09
to
I think SqlDatasource won't directly work with Label control. But it will
work with the combination of GridView control. I came to this decision just
because of a logical reason. You can find the smart tag in Data controls
such as GridView, DataList to select the datasource and so on. But you won't
find such options for label control. The reason is Gridview is specialised
controls for displaying the data whereas the label controls are used for
just displaying (may be statically or dynamically from the code). May reply
is going too long.

My suggestion is you can use datareader to display the text from the
datasource.
here is an example for you. I think this would help you.

Language: C#
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand sqlcmd = new SqlCommand("Query", conn);
conn.Open();
SqlDataReader sqldr = sqlcmd.ExecuteReader();
sqldr.Read();
label1.text=dr["ColumnName or index of the column"].toTrim.Tostring();
sqldr.close();
con.close();

Note: Use toTrim() while retriving data from the column with nchar or
nvarchar because this datatypes inserts empty spaces. this could raise
exception when you convert the string value into int or other datatypes.

I think my reply will help you to solve the problem.

Vijay chandar,
VC#,ASP.NET & SQLSERVER developer.

"Jeffrey Walton" <Jeffre...@discussions.microsoft.com> wrote in message
news:3F4B1B17-BA52-47C0...@microsoft.com...

Mark Rae [MVP]

unread,
Nov 29, 2009, 10:59:16 AM11/29/09
to
"Jeffrey Walton" <Jeffre...@discussions.microsoft.com> wrote in message
news:3F4B1B17-BA52-47C0...@microsoft.com...

> When I try to use the SqlDataSource as a standalone component, the label

> is
> not populated with text from the result of Eval(). How does one retrieve
> the
> fields? Or must the SqlDataSource only be used with DataViews, GridViews,
> etc?

http://www.mikesdotnetting.com/Article/64/Bind-Data-From-a-SqlDataSource-to-a-Label


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mark Rae [MVP]

unread,
Nov 29, 2009, 11:15:20 AM11/29/09
to
"Vijay" <vijaycha...@gmail.com> wrote in message
news:uylFUKQc...@TK2MSFTNGP06.phx.gbl...

[please don't top-post]

>> When I try to use the SqlDataSource as a standalone component, the label
>> is not populated with text from the result of Eval(). How does one
>> retrieve the fields? Or must the SqlDataSource only be used with
>> DataViews, GridViews, etc?

> I think SqlDatasource won't directly work with Label control.

It will:
http://www.mikesdotnetting.com/Article/64/Bind-Data-From-a-SqlDataSource-to-a-Label


> My suggestion is you can use datareader to display the text from the
> datasource.

Completely unnecessary - see above...


> label1.text
> sqldr.close();
> con.close();

The above will not compile because C# is case-sensitive.


> label1.Text=dr["ColumnName or index of the column"].toTrim.Tostring();

There are several problems with this line:

1) You need to convert an object to a string *BEFORE* you can use any of the
methods in the string class.

2) C# is case-sensitive, so .Tostring() will not compile.

3) There is no .toTrim method of the string class and, even if there were,
the brackets are missing.

The correct syntax would be:
label1.Text = dr["ColumnName or index of the column"].ToString().Trim();


> Note: Use toTrim() while retriving data from the column with nchar or
> nvarchar because this datatypes inserts empty spaces. this could raise
> exception when you convert the string value into int or other datatypes.

1) Again, there is no method toTrim() of the string class.

2) It is the char and nchar datatypes which are of fixed length, not varchar
and nvarchar.


As mentioned, it is not necessary to query the database twice in this case.
However, if you really want to do it, the code would be something like this:

using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand sqlcmd = new SqlCommand("Query", conn))
{
conn.Open();
using (SqlDataReader sqldr = sqlcmd.ExecuteReader())
{
if (sqldr.HasRows)
{
sqldr.Read();
Label.Text = sqldr["ColumnName or index of the
column"].ToString().Trim();

Vijay

unread,
Nov 29, 2009, 12:10:44 PM11/29/09
to
Mark, absolutely you are right. I wrote this code in an text editor and not
in a VS editor. I like to give an idea to Walton and not to use this exact
code.

"Mark Rae [MVP]" <ma...@markNOSPAMrae.net> wrote in message
news:#xCgo8Qc...@TK2MSFTNGP02.phx.gbl...

0 new messages