First, you import namespaces: System.Data and System.Data.SqlClient. Detailed code to see the source program.
Copy Code code as follows:
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script language= "C #" runat= "Server" >
protected void Page_Load (Object Src, EventArgs E)
{
SqlConnection myconn = new SqlConnection ("server=localhost;uid=sa;pwd=;d atabase=pubs");
Create Object SqlConnection
String strsql= "Select Au_id,au_lname,au_fname,phone,address,city,zip from Authors";
SqlDataAdapter mycmd = new SqlDataAdapter (strSQL, myconn);
Create Object SqlDataAdapter
DataSet ds = new DataSet ();
Create Object DataSet
Mycmd.fill (DS);
Populating data to a dataset
DataView Source = new DataView (ds. Tables[0]);
Mydatagrid.datasource = source;
Mydatagrid.databind ();
Binding Data to a DataGrid
}
</script>
<body>
</font><asp:datagrid id= "Mydatagrid" runat= "Server"
Width= "600"
Backcolor= "#ccccff"
Bordercolor= "BLACK"
Showfooter= "false"
Cellpadding=3
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
Maintainstate= "false"
/>
</body>