ObjectDataSource in ASP.Net

ObjectDataSource belongs to the family of data source controls in ASP.NET, which enables a declarative databinding model against a variety of underlying data stores, such as SQL databases or XML. The ObjectDataSource control allows developers to structure their applications using this traditional three-tiered architecture and still take advantage of the ease-of-use benefits of the declarative databinding model in ASP.NET.

<asp:ObjectDataSource ID='ObjectDataSource1' runat='server'>

Binding Data in GridView using ObjectDataSource:

<asp:GridView ID='GridView1' runat='server' DataSourceID='ObjectDataSource1' DataKeyNames='id'>
    <asp:ObjectDataSource ID='ObjectDataSource1' runat='server' TypeName='Class1'
          <asp:ControlParameter Name='id' ControlID='GridView1'/>

Here TypeName='Class1' class1 is a .cs file in which CreateConnection function is created.

ControlID='GridView1' name of control in which data will be show.

public static DataSet CreateConnection(string id)
string connectionstring = ConfigurationManager.ConnectionStrings['mycon'].ConnectionString;
SqlConnection con = new SqlConnection(connectionstring);
string queryString;
queryString = 'select id,pass,name  from [regform]';
SqlCommand dbCommand = new SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(dbCommand);
DataSet ds = new DataSet();
return ds;


            <add name='mycon'connectionString='Data Source=(local);Initial Catalog=my; User Id=sa; Password=mindstick'providerName='System.Data.SqlClient'/>

Write this connection string above <system.web>


ObjectDataSource in ASP.Net

ObjectDataSource in ASP.Net

Last updated:9/7/2019 12:11:31 AM


Leave Comment