Using the ObjectDataSource control in ASP.net


In this article I'm trying to explain how to work with ObjectDataSource control for displaying database data to our DataControls. During this process we can able to learn how to configure ObjectDataSource control and how to connect with DataControl. This is very simple to communicate with our Database.

Using the ObjectDataSource control in ASP.net :



In this article I'm trying to explain how to work with ObjectDataSource control for displaying database data to our DataControls. During this process we can able to learn how to configure ObjectDataSource control and how to connect with DataControl. This is very simple to communicate with our Database.

Follow below steps to configure ObjectDataSource control in our Application.

Step-1:



Add a class file to the project by right click on project and then choose items and then Add newItem and then choose a class file available in the list like below and give a name for that and click on Add button.

1

Step-2:



If you click Add button it's automatically ask the confirmation message for saving location purpose. It has been saved in App_code folder when you click “yes".

2

Step-3:



Wrote below lines of code under that class.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;


///
/// Summary description for Class1
///

public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public DataSet Get_Data()
{
SqlConnection con = new SqlConnection("connectionstring");
SqlDataAdapter da = new SqlDataAdapter("select * from tblEmployee", con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}




Step-4:



Right click on Project folder and then create new item choose webForm and give a name for that as ObjectDataSource.aspx.

3

Step-5:



Drag & drop ObjectDataSource control to the Design page and click on smart tag then choose Configure DataSource option as shown in below.

4

Step-6:



Select the classname Choose your business object drop down and then click on Next button.

5

Step-7:



Now, select the method name created under the class file then select that method and click on Finish button.

6

Step-8:



Drag & drop Gridview control on the design view and then choose DataSource then click on Ok button and then see the Gridview changes.

7

Step-9:



After configure Gridview control with ObjectDataSource see the source code of Gridview control.

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="Get_Data" TypeName="Class1"></asp:ObjectDataSource>


<br />
<asp:GridView ID="GridView1" runat="server"

DataSourceID="ObjectDataSource1">


</asp:GridView>


Step-10:



Now we can successfully configure object DataSource with gridview control, just press on F5 button to execute the program and see the output.

8

Conclusion:



I hope this article will help you those who are looking for the same and those who are new to implement ObjectDataSource control in your applications.

About Author:



This is Naveen, I completed my MCA and I have around 3 years of experience in ASP.net while implement Web Applications. For past 3 years I learn no.of Technologies while implement my applications apart from that I try to learn new technologies and I try to implement that while implement my project.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: