Different Way of Using ObjectDataSource
An ObjectDataSource control can represent any type of object in the .NET Framework. ObjectDataSource control use with collections,ADO.NET Data Reader, Dataset, LINQ to SQL queries and web services.
The ObjectDataSource control includes two properties named TypeName and SelectMethod in all below example. TypeName property contains the name of the component that you want to represent with the objectDataSource control. SelectMethod property represents the method of the component that you want to call when selecting data.
Download Complete Source code here
Using Collections :-
This is an simple collection example. Where its display an Customer Name using List collections.
ObjDSCollection class looks like below one
Using DataReader :-
The ADO.NET DataReader object provides you with a fast,read-only representation of database data. If you need to retrieve database
records in the fastest possible way, then you should use a DataReader object.
ObjDataReader class looks like below one :-
I had use three ADO.NET objects :- Connection,Command, and DataReader objects.Command object uses the connection object to connect to database. Then records are returned from command object and used by DataReader object. WebConfigurationManager is used to retrieve the database connection string from web configuration file.
Using DataSet :-
You also ca use the ObjectDataSource when you need to represent to ADO.NET DataSet. Using a DataSet is slower than using a DataReader.
However, you can perform advanced operations, such as filtering and sorting, on data represented with a DataSet.
ObjDataSet class looks like below one :-
Using LINQ :-
LINQ to SQL is the preferred method of data access in the .NET Framewor 3.5. You have to first create an CustomerLINQDataContext. The
easitest way to create the DataContext is to select LINQ to SQl Classes template. Go to menu-->Website-->Add New Item and select LINQ to SQL Classes template. Name the LINQ to SQl Classes CustomerLINQ.
ObjLINQ class looks like below one :-
Customer.dbml looks like below one.
Using Web Service :-
Web services enable you to share information across the Internet. When you communicate with a remote web service, you use a local proxy class
to represent the web service located on the remote machine. You can use the ObjectDataSource to represent this proxy class.
ObjWebService web service look like below :-
Output of all the above except Collection Demo is as below :-
I hope this will help you in using ObjectDataSource in different way.