zion
zion

Reputation: 11

databinding directly to a sqldatasource

in Asp.net C# I'm simply tring to bind a control to a sqldatasource that returns ONE record (one row).

<asp:SqlDataSource ID="sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT * FROM [Data_Events] WHERE ([Entry_ID] = @Entry_ID)"
    UpdateCommand="UPDATE Data_Events SET Resolution = @Resolution, Date_Resolved =       @Date_Resolved WHERE (Entry_ID = @id)">
<SelectParameters>

  <tr>
<td>Author</td>
<td><%# DataBinder.Eval(sqldatasource1, "Author")%></td>
<td>Incident Date</td>
<td><%# DataBinder.Eval(sqldatasource1, "Date_Incident")%></td>

<%# DataBinder.Eval(sqldatasource1, "Name")%>

This code is failing, what is the correct way?

Upvotes: 1

Views: 3739

Answers (3)

Mike Brind
Mike Brind

Reputation: 30035

You need to call the Select method of the DataSource control in code behind to get it to execute the SelectCommand. Once you have done that, you can access the resulting data, but how you do that depends on whether the DataSource is in DataSet or DataSource mode.

Here - I've written an article about it: Bind Data From a SqlDataSource to a Label

Upvotes: 0

Abe Miessler
Abe Miessler

Reputation: 85046

Typically if you want to databind to a SQLDatasource directly you would do it this way:

SqlDataSource sds = GetMyDataSource();
ListView lv_MyData = new ListView();
lv_MyData.DataSource = sds;
lv_MyData.DataBind();

If you want to databind to a DataSourceID on the aspx page you can do it like this:

<asp:ListView ID="lv_MyData" runat="server" DataSourceId="sds_MyData">
...
</asp:ListView>

<asp:SqlDataSource ID="sds_MyData" .... />

Without more information in your question there isn't much more I can tell you.

UPDATE:

It seems to me like you are taking the wrong approach to do this. I would recommend using ADO.NET or similar to execute your query from the code behind and set the value for a textbox or label on your aspx page.

The example at the end of the SqlCommand documentation provides an excellent example of how to go from a query to outputting results. I would recommend starting there:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

Upvotes: 2

Rick Liddle
Rick Liddle

Reputation: 2714

(This is off the cuff, and I can back it up with specific details if needed)

The DataSource controls expose a collection of records or objects, and DataBinder expects to iterate over that collection. In your example, the Eval() method is going to want to retrieve the "Name" field from the current index in the collection.

I assume you're trying to bind to a control like a label or textbox, and not one of the templated controls that would iterate over a DataSource. In that case, DataBinder.Eval() would be seeking the "Name" field from the current index, but there isn't a valid current index because there is no context for it.

Post back if that doesn't make sense and you want clarification. I'll elaborate if needed.

UPDATE:

In the example code, you've got a row from a table. Without some sort of templated control (like a repeater) surrounding it, there is no context for the binding. It's like asking a collection object for a value from one of its elements without specifying which element to retrieve the value from.

Upvotes: 1

Related Questions