473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

using asp.net databinding controls is more efficient or buildingstrings manually?

Hi, Which one has more performance and speed? using dataset and
databinding asp.net controls or building string by SqlDataReader ?

the following is my two methods:
1. Using SqlDataReader :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="default.aspx.cs" Inherits="homepage" %>
<html>
<body>

<%=HomepageNews %>

</body>
</html>

CodeBehinde:
----------------------
public string HomepageNews()
{
SqlConnection connection = new
SqlConnection(_connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetTop5News";

string news = "";

connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
news += @"<tr>
<td>" + reader["Title"].ToString() + @"</td>
<td rowspan=2>" + reader["Image"].ToString() + @"</
td>
</tr>
<tr>
<td>" + reader["Brief"].ToString() + @"</td>
</tr>";
}
reader.Close();
connection.Close();

return ("<table>" + news + "</table>");
}

2. Using DataSet and Repeater :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="default.aspx.cs" Inherits="homepage" %>
<html>
<body>
<form id="HomepageForm" runat="server">
<asp:Repeater id="CommentsRepeater" runat="server">
<HeaderTemplate<table</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("Title") %></td>
<td rowspan="2">
<%# Eval("Image") %></td>
</tr>
<tr>
<td>
<%# Eval("Brief") %></td>
</tr>
</ItemTemplate>
<FooterTemplate</table</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
CodeBehinde:
----------------------
protected void Page_Load(object sender, EventArgs e)
{
CommentsRepeater.DataSource = TopFiveNews();
CommentsRepeater.DataBind();
}

private DataSet TopFiveNews()
{
SqlConnection connection = new
SqlConnection(_connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetTop5News";

SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
Sep 20 '08 #1
2 1661
Hi

Why not use DataReader + Reapter? Like this:

<asp:SqlDataSource runat="server" ID="data" DataSourceMode="DataReader" ...
<asp:Repeater DataSourceID="data" ...

Regards
Warren
Sep 20 '08 #2
building a string directly is the most efficient (but probably not
noticeable). using a datareader directly is risky and can lead to memory
leaks (like your sample code doesn't release resources on an exception).

-- bruce (sqlwork.com)

hp*****@yahoo.com wrote:
Hi, Which one has more performance and speed? using dataset and
databinding asp.net controls or building string by SqlDataReader ?

the following is my two methods:
1. Using SqlDataReader :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="default.aspx.cs" Inherits="homepage" %>
<html>
<body>

<%=HomepageNews %>

</body>
</html>

CodeBehinde:
----------------------
public string HomepageNews()
{
SqlConnection connection = new
SqlConnection(_connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetTop5News";

string news = "";

connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
news += @"<tr>
<td>" + reader["Title"].ToString() + @"</td>
<td rowspan=2>" + reader["Image"].ToString() + @"</
td>
</tr>
<tr>
<td>" + reader["Brief"].ToString() + @"</td>
</tr>";
}
reader.Close();
connection.Close();

return ("<table>" + news + "</table>");
}

2. Using DataSet and Repeater :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="default.aspx.cs" Inherits="homepage" %>
<html>
<body>
<form id="HomepageForm" runat="server">
<asp:Repeater id="CommentsRepeater" runat="server">
<HeaderTemplate<table</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("Title") %></td>
<td rowspan="2">
<%# Eval("Image") %></td>
</tr>
<tr>
<td>
<%# Eval("Brief") %></td>
</tr>
</ItemTemplate>
<FooterTemplate</table</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
CodeBehinde:
----------------------
protected void Page_Load(object sender, EventArgs e)
{
CommentsRepeater.DataSource = TopFiveNews();
CommentsRepeater.DataBind();
}

private DataSet TopFiveNews()
{
SqlConnection connection = new
SqlConnection(_connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetTop5News";

SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
Sep 20 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Johann Blake | last post by:
I've been playing around trying to bind textbox controls and datagrids to typed datasets. Up until recently, I never used DataBindings. I always manually wrote the value into a textbox and stored...
4
by: Anders | last post by:
Hi, I was wondering what is most efficient of the two. Is it more efficient to add server controls within the Itemtemplate and use OnItemDataBound to manipulate and databind the servercontrols. ...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
19
by: Larry Lard | last post by:
In the old days (VB3 era), there was a thing called the Data Control, and you could use it to databind controls on forms to datasources, and so (as the marketing speak goes), 'create database...
1
by: boomhauer | last post by:
I cant seem to find info online about this so thought id ask.. I have an asp.net 2.0 page that has: 1. an infragistics calendar control 2. a DDL that has an ODS with a param pulled from the...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
2
by: Don Miller | last post by:
I have controls (dropdown list, radiobutton list) that are bound to an SQLDataSource. In the SELECT statement in the DataSource configuration I have tried to include other columns that would be...
1
by: Mark Olbert | last post by:
Has anyone else noticed that the design-time support for databinding in custom controls in ASPNET2 sucks? At least for GridViews? So far I've spent going on two days trying to get the following...
2
by: cjard | last post by:
I ask, because I have a textbox bound to a bindingsource, which is bound to a datatable(row) The datatable is typed, and hence exports a Property called Column1 I added another property to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.