ASP.Net C# Repeater Databinding using DataSource

by top54u.com 14 Jul, 2008

ASP.Net Repeater Databound Control Databinding feature displays the data items retrieved from any DataSource in the specified repeated list layout. Here we will discuss the ASP.Net C# code to connect with SQL Server Database and Repeater control databinding using its DataSource object that gets or sets the data provided by the data source for populating the repeated list items. In the previous article about ASP.Net Repeater Control we discussed about 5 types of templates that can be sued to customize the layout of Repeater DataBound Control. ItemTemplate field is a required template to display the data retrieved from the DataSource. You can specify the heading for data list items in the HeaderTemplate.

 

In this Repeater Control Tutorial we will use Northwind SQL database to bind the data with Repeater Control. Define the Following connectionstring in the web.config file’s ConnectionStrings section:

 

<add name="ConStr"

connectionString="Server=localhost; Database=northwind; uid=sa; pwd=;"

providerName="System.Data.SqlClient">

</add>

 

HTML Code for Repeater Control and DataBinder.Eval Function

 

<div>

<asp:Repeater ID="Repeater1" runat="server">

<HeaderTemplate>

<h4>Emplyee Names</h4>

</HeaderTemplate>

<ItemTemplate>

<%#DataBinder.Eval(Container.DataItem,"FirstName") %> <%#DataBinder.Eval(Container.DataItem,"LastName") %>

<br />

</ItemTemplate>

<SeparatorTemplate>

<hr />

</SeparatorTemplate> </asp:Repeater>

</div>

 

 

DataBinder class provides the Eval function that accepts two parameters. First parameter value as a reference against the DataItem object using Container variable of as RepeaterItem that is to be evaluated. Second parameter is a string type expression that passes the name of Data Field retrieved from the DataSource.

 

ASP.Net C# Code for Repeater Databinding using DataSource

 

 

// string variable to store the connection string

// defined in connectionstrings section of web.config file.

string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

 

 

// object created for SqlConnection Class.

SqlConnection mySQLconnection = new SqlConnection(connStr);

 

 

// if condition that can be used to check the sql connection

// whether it is already open or not.

if (mySQLconnection.State == ConnectionState.Closed)

{

mySQLconnection.Open();

}

 

 

// SQL Select Command

SqlCommand mySqlSelect = new SqlCommand("select * from employees", mySQLconnection);

mySqlSelect.CommandType = CommandType.Text;

SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);DataSet myDataSet = new DataSet();

mySqlAdapter.Fill(myDataSet);

 

// Repeater Control Databinding using Datasource

Repeater1.DataSource = myDataSet;

Repeater1.DataBind();

 

 

// if condition that can be used to check the sql connection

// if it is open then close it.

if (mySQLconnection.State == ConnectionState.Open)

{

mySQLconnection.Close();

}

 

 

Above Code will generate the following HTML source code

 

<div>

<h4>Emplyee Names</h4>

Nancy Davolio

<br />

<hr />

Andrew Fuller

<br />

<hr />

Janet Leverling

<br />

<hr />

Margaret Peacock

<br />

<hr />

Steven Buchanan

<br />

<hr />

Michael Suyama

<br />

<hr />

Robert King

<br />

<hr />

Laura Callahan

<br />

<hr />

Anne Dodsworth

<br />

</div>

 

Output:

 

Above output result of ASP.Net repeater control shows that Repeater control does not render extra HTML tags like <table> HTML tag in DataList or GridView Databound controls of ASP.Net

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:24:17 AM




related videos.....
recent posts.....
top54u ezines.....