Click here to Skip to main content
15,891,409 members
Articles / Web Development / ASP.NET
Article

Accessing Data Using Object Data Source in ASP.net 2.0 and C#

Rate me:
Please Sign up or sign in to vote.
1.11/5 (15 votes)
21 Aug 20062 min read 56.9K   14   6
To access data base and read and bind data using objcect data source

Introduction

Accessing Data Using Object Data Source (ASP.net 2.0) in C#

 <o:p>

One of the best Methods to read or write data is Data binding against objects.<o:p>

 <o:p>

DataBinging against Objects using Visual Studio 2005 .<o:p>

 <o:p>

1.Create App_Code Folder in your Project root.<o:p>

2.right-click on the App_Code and select Add->Add New Item to add a new DataSet

   called "Sample.xsd" to the project.<o:p>

 <o:p>

<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><o:lock v:ext="edit" aspectratio="t">

Sample image 

 

3.Double click on DataSet Sample.xsd . You will get a tool box for your
   
Data set.
4.Grab a Table Adapter control .<o:p>

You may see another Table Adapter as shown below .<o:p>

 

Sample image

 

5. Create appropriate connection string or select already created one.<o:p>

6. Click Next and tick Use Sql Statements  and Write a Query to select the rows.<o:p>

     You can use the Query Builder for automatic queries and check the results there it

     self.<o:p>

7. Rename DataTable1 to Sample  (for an example) . Which renames the adapter

    also. <o:p>

8. After successful creation of the sql query which creates a Method by default say

    GetData().<o:p>

 <o:p>

Right-click on the App_Code and select Add->Add New Item to add a new Class file called "AssignValues.cs" to the project.<o:p>

<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><o:lock v:ext="edit" aspectratio="t"> 

Sample image

 <o:p>

Double Click on the class and create a method in it

 <o:p>

public class AssignValues<o:p>

{

 <o:p>

   public void Fill(GridView gdv)<o:p>

    {<o:p>

 <o:p>

      gdv.ID = string.Empty;<o:p>

      SampleTableAdapters.MemberTableAdapter mtadptr = null;               <o:p>

      mtadptr = new SampleTableAdapters.MemberTableAdapter();<o:p>

      Sample.MemberDataTable dt=null;<o:p>

      dt=mtadptr.GetData();<o:p>

      gdv.DataSourceID=string.Empty;<o:p>

      gdv.DataSource=dt;<o:p>

       <o:p>

      gdv.DataBind();<o:p>

    }<o:p>

}<o:p>

 <o:p>

Explanation for the above code:

 <o:p>

Grid view is an object parameter you passing it from in which event you need to fill the data.Then creating an object of  Sample data set dt and filling the data  into it and bind it.

 

After that you can make an instance of the class and call the method in any other c# code page of the Project files. Pass the parameter Grid View say GridView1 after grabbing a Grid view from the Tool Box.

 <o:p>

 

 <o:p>

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I am Working as a Software Engineer. My interests are in asp.net 2.0 and C#.I am an MCA and currently concentrating on Microsoft Technologies.
praveen.theendakkara@marlabs.com


Comments and Discussions

 
GeneralNeed to change the contents !! Pin
solai rajan9-Aug-09 2:18
solai rajan9-Aug-09 2:18 
GeneralMy vote of 1 Pin
Member 399425929-Jun-09 6:01
Member 399425929-Jun-09 6:01 
GeneralMy vote of 1 Pin
Member 161769625-Nov-08 9:31
Member 161769625-Nov-08 9:31 
GeneralGetting error with this procedure Pin
Newbie923932919333112-Dec-07 17:48
sussNewbie923932919333112-Dec-07 17:48 
Generalworking sh*t Pin
Member 38086228-Feb-07 21:01
Member 38086228-Feb-07 21:01 
Generalvery bad article Pin
deepaktripathi31-Jan-07 21:33
deepaktripathi31-Jan-07 21:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.