views:

154

answers:

2

Is it possible using Eval? If so, can someone post a short fragment to illustrate? My SqlDataSource returns 1 record with 3 fields, I'd like to Format these into a string. eg:

Record Field 'Name' = 'Jack' Field 'Amount' = 100 Field 'Date' = 12.02.2010

asp:Label text should end up being:

Welcome Jack, last payment was 100 on 12.02.2010

if another control than asp:Label is appropriate, that would be good to know also.

+1  A: 

Label's the right control for what you want to display, but SqlDataSource probably isn't the right control for getting the data. Labels have no DataSourceID property that allows you associate the controls together - whilst you might be able to do some plumbing in your server-side code to make it work, my suspicion is it'd be more efficient to get the relevant DataRow during Page_Load, build the string and then put that into the Text property of the Label.

PhilPursglove
Thanks - do you mean open the database with DAO and execute the query manually?
cskilbeck
+1  A: 

Thanks for the steer Phil, this is my solution (error checking to do, critique welcome):

private void DataSourceIntoLabel(Label label,
                                SqlDataSource dataSource,
                                ParameterCollection parameters,
                                string formatString,
                                string[] columnNames)
{
    dataSource.SelectParameters.Clear();
    foreach (System.Web.UI.WebControls.Parameter p in parameters)
    {
        dataSource.SelectParameters.Add(p);
    }
    IEnumerable rows = dataSource.Select(DataSourceSelectArguments.Empty);
    IEnumerator enumerator = rows.GetEnumerator();
    enumerator.MoveNext();
    DataRowView rowView = (DataRowView)enumerator.Current;
    label.Text = string.Format(formatString, rowView.Row.ItemArray);
}
cskilbeck
columnNames is unused - hangover from previous iteration.
cskilbeck
Nice one, glad I could help!
PhilPursglove