While fetching data from the sources like the text file , the xml file or from the database first thing you need to do is to create a data source for it.
Data Source acts like a object of information that holds information about the settings and path that we will use to fetch the data from the selected data source.
Main work of data source :
- Managing connection
- Selecting data
- Managing presentation aspects like paging, caching, etc.
- Manipulating data
The data source controls used for hierarchical data are:
The SqlDataSource Control
Mainly used while interacting with the Sql Server database and tables associated to it.
<asp:SqlDataSource runat="server" ID="MySqlSource"
ProviderName='<%$ ConnectionStrings:LocalNWind.ProviderName %>'
ConnectionString='<%$ ConnectionStrings:LocalNWind %>'
SelectionCommand= "SELECT * FROM EMPLOYEES" />
<asp:GridView ID="GridView1" runat="server" DataSourceID="MySqlSource" />
The ObjectDataSource Control
Object data source control is mainly concerned with the data of object kind like the models and entities.
public class Student
{
public int StudentID { get; set; }
public string Name { get; set; }
public string City { get; set; }
public Student()
{ }
public DataSet GetStudents()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Students");
dt.Columns.Add("StudentID", typeof(System.Int32));
dt.Columns.Add("StudentName", typeof(System.String));
dt.Columns.Add("StudentCity", typeof(System.String));
dt.Rows.Add(new object[] { 1, "M. H. Kabir", "Calcutta" });
dt.Rows.Add(new object[] { 2, "Ayan J. Sarkar", "Calcutta" });
ds.Tables.Add(dt);
return ds;
}
}
0 Comment(s)