The main reason that you wouldn’t want to use inline binding is performance. Inline binding uses reflection to determine what you’re trying to how, and this is slower than using the DataReader and DataSet directly.
Another reason to avoid inline binding is code maintenance. If you use inline binding, you are mixing real code with the HTML using <% and %> with the result being the most horrible spaghetti code. Look below the difference:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html>
<script runat="server">
SqlDataReader myReader;
protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
string CommandText = "SELECT ManufacturerName, ManufacturerCountry, ManufacturerEmail, ManufacturerWebsite FROM Manufacturer WHERE ManufacturerID = 1";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
try
{
myConnection.Open();
myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
Page.DataBind();
}
else
{
lblError.Text = "No results to databind to.";
}
}
finally
{
myConnection.Close();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Inline DataReader</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Label ID="lblName" runat="server"></asp:Label>
Name: <%# DataBinder.Eval(myReader, "[ManufacturerName]") %>
<br />
Country: <asp:Label ID="lblCountry" runat="server" Text='<%# DataBinder.Eval(myReader, "[ManufacturerCountry]") %>'></asp:Label>
<br />
Email: <asp:HyperLink ID="lnkEmail" runat="server" NavigateUrl='<%# DataBinder.Eval(myReader, "[2]", "mailto:{0}")%>' Text='<%# DataBinder.Eval(myReader, "[ManufacturerEmail]") %>'></asp:HyperLink>
<br />
Website: <asp:HyperLink ID="lnkWebsite" runat="server" NavigateUrl='<%# DataBinder.Eval(myReader, "[3]") %>'><%# DataBinder.Eval(myReader, "[ManufacturerWebsite]") %></asp:HyperLink>
<br /><br /><asp:Label ID="lblError" runat="server"></asp:Label>
</div>
</div>
</form>
</body>
</html>
You will display the same data from the DataReader without relying on inline binding. The benefit is that these changes have made the split between what is code and what is presentation a lot easier to see.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html>
<script runat="server">
SqlDataReader myReader;
protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
string CommandText = "SELECT ManufacturerName, ManufacturerCountry, ManufacturerEmail, ManufacturerWebsite FROM Manufacturer WHERE ManufacturerID = 1";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
try
{
myConnection.Open();
myReader = myCommand.ExecuteReader();
if(myReader.Read())
{
lblName.Text = Convert.ToString(myReader["ManufacturerName"]);
lblCountry.Text = Convert.ToString(myReader["ManufacturerCountry"]);
lnkEmail.Text = Convert.ToString(myReader["ManufacturerEmail"]);
lnkEmail.NavigateUrl = "mailto:" + Convert.ToString(myReader["ManufacturerEmail"]);
lnkWebsite.Text = Convert.ToString(myReader["ManufacturerWebsite"]);
lnkWebsite.NavigateUrl = Convert.ToString(myReader["ManufacturerWebsite"]);
}
else
{
lblError.Text = "No results to databind to.";
}
}
finally
{
myConnection.Close();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server"></asp:Label>
<br />
Country: <asp:Label ID="lblCountry" runat="server"></asp:Label>
<br />
Email: <asp:HyperLink ID="lnkEmail" runat="server"></asp:HyperLink>
<br />
Website: <asp:HyperLink ID="lnkWebsite" runat="server"></asp:HyperLink>
<br /><br /><asp:Label ID="lblError" runat="server"></asp:Label>
</div>
</form>
</body>
LT