<%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <%@ Page Language="C#" Debug="true" %> <script runat="server"> void Page_Load(object sender,EventArgs e) { //'*** Create Object Session ***// Object objFSO, objStream; objFSO = Server.CreateObject("Scripting.FileSystemObject"); //*** Used Session ***// Session["mySession"] = objFSO; objStream = Session["mySession"].OpenTextFile(Server.MapPath("MyFiles/thaicreate.txt")); while (!(StrWer.EndOfStream)) { this.lblText1.Text = this.lblText1.Text + objStream.ReadLine() + "<br>"; } objStream.Close(); //*** New Control & Session ***// OleDbConnection objConn; OleDbCommand objCmd; String strConnString; strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/mydatabase.mdb") + ";"; objConn = new OleDbConnection(strConnString); objConn.Open(); String strSQL; strSQL = "SELECT * FROM customer"; OleDbDataReader dtReader; objCmd = new OleDbCommand(strSQL, objConn); dtReader = objCmd.ExecuteReader(); //*** DataGrid Session ***// DataGrid myDataGrid = new DataGrid(); Session["myControl"] = myDataGrid; DataGrid myCtrl = (DataGrid)Session["myControl"]; myCtrl.AutoGenerateColumns = true; myCtrl.DataSource = dtReader; myCtrl.DataBind(); //*** Load Control To Label ***// Me.lblText2.Controls.Add(myCtrl); dtReader.Close(); dtReader = null; objConn.Close(); objConn = null; } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - Session Object</title> </head> <body> <form runat="server"> <asp:Label id="lblText1" runat="server"></asp:Label> <hr /> <asp:Label id="lblText2" runat="server"></asp:Label> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท