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">
</asp:ObjectDataSource>


Binding Data in GridView using ObjectDataSource:

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

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();
da.Fill(ds);
return ds;
}

web.config:

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

Write this connection string above <system.web>

 

ObjectDataSource Control in ASP.Net

ObjectDataSource Control in ASP.Net

  Modified On Mar-22-2018 07:25:49 AM

Leave Comment