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>
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...
> 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
[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();
"Mark Rae [MVP]" <ma...@markNOSPAMrae.net> wrote in message
news:#xCgo8Qc...@TK2MSFTNGP02.phx.gbl...