Retrieving Data from a SqlDataSource Control

There are plenty of examples of how to create a SqlDataSource control in ASP.NET 2.0 and how to then bind that control as the datasource for another control, but they’re aren’t any that will show you how to retrieve data from the control to use in code. The following code will write each of the values from the “FieldName” column to the page (be sure the DataSourceMode property of the SqlDataSource control is set to DataSet):

Dim oDV As System.Data.DataView

oDV = SqlDataSource1.Select(DataSourceSelectArguments.Empty)

For Each oDR As System.Data.DataRow In oDV.Table.Rows
Response.Write(oDR(“FieldName”))
Next

If your SqlDataSource returns a single row, you can reference the values directly (the Rows object uses a zero-based index to retrieve the row):

Response.Write(oDV.Table.Rows(0)(“FieldName”))

Leave a Reply

Your email address will not be published. Required fields are marked *