try declaring the
SQLDataSource
in the web form code, instead of doing it in the code behind.
then set the datasource to the GridView using the
DataSourceID
property.
if you need to later modify the query, just change it directly from the
SqlDataSource
object and call
Rebind()
on the grid to update results, no need for you to open/close any connections.
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:YourConnectionString%>"
SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
runat="server"
DataSourceID="SqlDataSource1">
your columns here..
</asp:GridView>