Search
Author
ASP.NET Tutorials
Author
Sheo Narayan
Advertisements


Winners

Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial
asp:SqlDataSource control
SqlDataSource control is a data source control that is used to connect to a relational database.
 
SqlDataSource control is a data source control that is used to connect to a relational database. You can even connect to Oracle or any other data source that can be accessible through OLEDB and ODBC. ConnectionString and ProviderName are the two properties that is used to connect the database. The first one specify the connection string and second specify the provider name. By default the provider is System.Data.SqlClient, it means connect to Sql Server database.

 

Following are some important properties that are very useful.
Properties for Configuring Data Source
InsertCommand, InsertParameters, InsertCommandType Gets or sets the SQL Statement, parameter and type of command (text or stored procedure) to insert a record.
DeleteCommand, DeleteParameters, DeleteCommandType Gets or sets the SQL Statement, parameters and type of the command (text or stored procedure) to delete a record.
UpdateCommand, UpdateParameters, UpdateCommandType Gets or sets the SQL Statement, parameters and type of the command (text or stored procedure) to update a record.
SelectCommand, SelectParameters, SelectCommandType Gets or sets the SQL Statement, parameters and type of the command (text or stored procedure) to select record(s).
Parameter Types of DataSource Controls
ControlParameter Gets the value from any public property of the server control.
FormParameter Gets the value of any input field from the form.
QueryStringParameter Gets the value from specified querystring.
Parameter Gets the parameter value assigned in the code.
Other Properties of SqlDataSource Control
ProviderName Gets or sets the .NET ProviderName name.
DataSourceMode DataSet/DataReader. Used to specify the data source mode. In case of DataReader paging is not supported in the target control (GridView etc.).
ConnectionString Gets or sets the connection string to connect to the database.
ConflictDetection CompareAllValues/OverWriteChanges. Used to determine how conflict will be handled in case of updation or deletion of the records.
Caching Properties of SqlDataSource Control
EnableCaching true/false. Used to indicate whether enable caching or not.
CacheDuration Used to indicate number of seconds the data shoud be maintained in the cache.
CacheExpirationPolicy Absolute/Sliding. Used to indicate if the cache policy is absolute or sliding.
Absolute: The data is removed after specified duration. Sliding: The data is removed if it is not used for specified duration.
EnableCaching true/false. Used to indicate whether enable caching or not.
DEMO : SqlDataSource Show Source Code
AutoID18170
Name 
Address') AND EXTRACTVALUE(3595,CONCAT(0x5c,0x7171787071,(SELECT (ELT(3595=3595,1))),0x716a786271)) AND ('FzVh'='FzVh
Phone 
City 
12345678910...
 
    
    // DetailsView Control ///////////////////////////////                    
    <asp:DetailsView ID="DetailsView1" runat="Server" CellPadding="4" ForeColor="#333333" GridLines="None"
     Width="100%" DataSourceID="SqlDataSource1" AllowPaging="True" AutoGenerateRows="True" DataKeyNames="AutoID">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
        <EditRowStyle BackColor="#999999" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:DetailsView>
    
    
    // SqlDataSource Control ///////////////////////////////
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnStr %>'
     SelectCommand="Select * FROM SampleForTutorials ORDER BY [Name]" DataSourceMode="DataSet">
     </asp:SqlDataSource>