ASP.net 2 SqlDataSource Control
SqlDataSource Control is used if your data is stored in a SQL Server, Oracle Server, ODBC data source, OLE DB data source, or Windows SQL CE Database.
We have to specify the provider name for the SqlDataSource Control in the declaration. There are 4 providers that come with .NET for this purpose. They are
- System.Data.SqlClient
- System.Data.OracleClient
- System. Data.Odbc
- System.Data.OleDb
Apart from the provider name, connection string and the query logic is to be specified.
The complete declaration of the DataSource Control is :
<asp:SqlDataSourec ID="ItemDataSource" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="server=localhost;database=DatabaseName;uid=user;pwd=password" SelectCommand="SELECT ItemID, ItemName from Item_Table"></asp:SqlDataSource>
Now add a Data-Bound control to use the data from the DataSource control.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LIFPD %>"
SelectCommand="SELECT [ID], [Itemname], [Description],
[Remarks] FROM [Test_Table]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Itemname" HeaderText="Itemname"
SortExpression="Itemname" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks"
SortExpression="Remarks" />
</Columns>
</asp:GridView>