This is a visual and easy way to build a data report, but it is not a time-saving way, and I’m sure most of experienced page developers would prefer the first approach, i.e. inline coding directly.
SqlDataSourceControl provide a fast and easy way to connect and define the command for data access. A new control come with Web Matrix (Version 0.5 Build 464) – SqlDataSourceControl, and the usage of this control and another new control – MxDataGrid – are closely related, which is also be described in the Web Matrix Guided Tour
(http://asp.net/webmatrix/tour/section3/binddatagrid.aspx)
When you connect to a data source and then drag and drop a Database table onto the designer pane of Web Matrix, a MxDataGrid will be used and the SqlDataSourceControl will be configured correctly and bind to the MxDataGrid, so a fully functional data report will be generated immediately.
Procedure:
1. Connect to your data source:
2. Drag your interested Database Table onto the Designer Pane of Web Matrix:
3. Completed: A SqlDataSourceControl and a MxDataGrid were generated, filled and binded automatically.
Data binding to other data display control with SqlDataSourceControl
Actually, the SqlDataSourceControl is NOT limited to the use of MxDataGrid ONLY. The SqlDataSourceControl provide a simple data source connection mechanism, we can only have to define the Select or Delete CommandText, and all of its properties can be found at the property window:
As you can see that the ConnectionString, SelectCommand or UpdateCommand can be modified in the property window at design time, or you may have another question right now: If I store the connection string in Web.Config file, or I want to dynamically change the SQL Select Statement, how can I do that?
The SqlDataSourceControl is basically a Server Control, though it encapsulates a lot of functions and codes, we are able to programmatically create an instance of it or change its properties in runtime.
E.g.
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
SqlDataSourceControl1.ConnectionString = ConfigurationSettings.AppSettings("connectionString")
End Sub
The nice thing of using SqlDataSourceControl is that you can create such control for data access once, and then you can use it repeatedly for other data display controls on the web form.
The following example showing the use of such control and data binding to different controls, like DataGrid, DropDownList, DataList and Repeater control. Finally, this SqlDataSourceControl is a very friendly control that saves us developers’ lots of time and code, compared with inline coding or even SqlHelper class. What we have to do is just drag and configure a SqlDataSourceControl, and then put it into any data binding control as:
<asp:Repeater id="Repeater1" runat="server"
DataMember="Table"
DataSource="<%# SqlDataSourceControl1 %>"
>
...
</asp:Repeater>
Download