<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>SqlConnection Using ConnectionString in Web.Config</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server">
string html;
protected void Page_Load ( Object src, EventArgs e ) {
string strConn = ConfigurationManager.ConnectionStrings [ "aspnet" ].ToString ( );
// connect to data source
SqlConnection myConn = new SqlConnection ( strConn );
// define the command query
SqlCommand myCmd = new SqlCommand ( "SELECT * FROM Books WHERE Subject = 'C#' AND Price IS NOT NULL", myConn );
// open the data connection
myConn.Open ( );
// bind the datalist to the reader
myList.DataSource = myCmd.ExecuteReader ( CommandBehavior.CloseConnection );
myList.DataBind ( );
// close the data connection
myConn.Close ( );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>SqlConnection Using <b>ConnectionString</b> in <b>Web.Config</b></h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<center>
<form runat="server">
<asp:datalist id="myList" runat="server" width=90%
repeatcolumns=2 repeatdirection="horizontal"
itemstyle-width=50%>
<itemtemplate>
<table width=92% align="center" cellpadding=10>
<tr>
<td width=25% valign="top">
<a href='<%# Eval ( "url" ) %>'
target="_blank"><img src='<%# Eval ( "titleid", "/shared/books/{0}.jpg" ) %>'
style="box-shadow: 3px 3px 3px gray"></a>
</td>
<td width=75% valign="top">
<p style="font:bold 14pt arial">
<a href='<%# Eval ( "url" ) %>'
target="_blank"><span style="color:darkgreen"><%# Eval ( "title" )%></span></a></p>
<b>Title ID:</b> <%# Eval ( "titleid" ) %><br>
<b>Author:</b> <%# Eval ( "author" ) %><br>
<b>Category:</b> <%# Eval ( "subject" ) %><br>
<b>Publisher:</b> <%# Eval ( "publisher" ) %><br>
<b>Price (from):</b> <%# Eval ( "price", "{0:c2}" ) %><p>
</td></tr>
</table>
</itemtemplate>
</asp:datalist>
</form>
</center>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>