Pupulate a DataGrid using SqlClient
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e )
{
SqlConnection myConn = new SqlConnection(
ConfigurationSettings.AppSettings["MyDatabase"]);
SqlCommand myCmd = new SqlCommand("select Nome, Cognome, Email from utenti", myConn);
try
{
myConn.Open();
myGrid.DataSource = myCmd.ExecuteReader();
myGrid.DataBind();
myConn.Close ( );
}
finally
{
myConn.Close();
}
}
</script>
<body>
<asp:datagrid id="myGrid" runat="server"
width="92%" cellpadding=5 font-size="8pt"
gridlines="horizontal"
headerstyle-backcolor="slategray"
headerstyle-forecolor="ivory"
itemstyle-verticalalign="top"
autogeneratecolumns=false>
<columns>
<asp:boundcolumn headertext="Nome"
datafield="Nome" />
<asp:boundcolumn headertext="Cognome"
datafield="Cognome" />
<asp:boundcolumn headertext="Email"
datafield="Email" />
</columns>
</asp:datagrid>
</body>
</html>
Download Code...
Print Page