Example DataAdapter : use DataAdapter in asp.net

DataAdapter Example :  This is a example of  dataadapter.


<%@ Page Language="C#" %>  
<%@ Import Namespace="System.Data" %>  
<%@ Import Namespace="System.Data.SqlClient" %>  
<%@ Import Namespace="System.Configuration" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    protected void Page_Load(object sender, System.EventArgs e) {  
        if (!Page.IsPostBack) {  
            SqlConnection MyConnection;  
            SqlCommand MyCommand;  
            SqlDataAdapter MyAdapter;  
            DataTable MyTable;  
  
            MyConnection = new SqlConnection();  
            MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings

["AppConnectionString1"].ConnectionString;  
  
            MyCommand = new SqlCommand();  
            MyCommand.CommandText = "SELECT TOP 8 * FROM PRODUCTS";  
            MyCommand.CommandType = CommandType.Text;  
            MyCommand.Connection = MyConnection;  
  
            MyTable = new DataTable();  
            MyAdapter = new SqlDataAdapter();  
            MyAdapter.SelectCommand = MyCommand;  
            MyAdapter.Fill(MyTable);  
  
            GridView1.DataSource = MyTable.DefaultView;  
            GridView1.DataBind();  
  
            MyAdapter.Dispose();  
            MyCommand.Dispose();  
            MyConnection.Dispose();  
              
        }  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>DataAdapter example: how to use DataAdapter in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
    </div>  
    </form>  
</body>  

</html>  

 

No comments:

Post a Comment