25 April 2008

ASP.Net & DataSource Controls


Data Source Controls & Data Source Consumed Controls

Generally it is not recommended to use data source controls. This is because of a believe that we will be limited inside the datasource framework.This is not true, datasource controls gives an abstract way of data manipulation , we can handle the process with the help of events provided by data source controls.
DataGridView is the most useful control which consumes datasource controls,it gives the options like sorting, paging etc.. in all of these events gridview will automatically call data-source control to retrieve data, in manual coding we need to call all proper query for all these events.
In some situations the SqlDatasource control can't use properly , at these situation we can use ObjectDataSource which is the powerful of all data source controls. It allow us to query a bulk record for paging .
We can even create a complete project without a single line of coding with ds-controls not only listing (selecting) records from table but also all data manipulation operation (insert update and delete ) with the help of DS-Controls. FormView-Control is the DS-Consumed Control that allow all the data manipulation operations with a windows form like environment.
Most people will leave this technology because of the customization problems arise with this technology,but actually it is very easy to handle these difficulties if you think a little different way .....
I will some tips that help you customize the data source controls and data-source consumed controls to achieve some risks you may find with coding .

Data-Source Controls and Parameters
For generating dynamic result with datasource controls we can pass parameters (control parameter , Query-string Parameter etc..)
Suppose you need to create a Search result from user table with a search patter from a TextBox named "FilterTextBox" , and your query is like this
" Select * From Users Where UserName like '%'+@Search +'%' "
so the parameter is @Search , Add a Select(Filter) Parameter with Name @Search and type as Control-Parameter and control-name as FilterTextBox
.
Control-Parameter is easy way to pass parameters , you can add a asp:label with visible=false as the parameter to hide sensitive informations like userId and you can pass the userId to this asp:label at the page_Load() event.

Nested DataSource Controls
Another benefit of datasource control is that we can place DataSource & Consumed-Controls inside a DataSource-Consumed Controls. We can pass Control-parameters To this controls from the derived data values.