SqlDataSource, Eval, and Label Text

  • Thread starter Thread starter Jeffrey Walton
  • Start date Start date
J

Jeffrey Walton

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>
 
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.
 
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 said:
[please don't top-post]
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();
}
}
}
}
 
Back
Top