<%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <%@ Page Language="C#" %> <script runat="server"> OleDbConnection objConn = new OleDbConnection(); OleDbCommand objCmd = new OleDbCommand(); string strConnString; string strSQL; public void Page_Load(object sender, EventArgs e) { strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/mydatabase.mdb") + ";Jet OLEDB:Database Password=;"; objConn.ConnectionString = strConnString; objConn.Open(); } public void btnSave_Click(object sender, EventArgs e) { int intNumRows = 0; strSQL = "SELECT COUNT(*) FROM member WHERE Username = '" + this.txtUsername.Text + "' "; objCmd = new OleDbCommand(strSQL, objConn); intNumRows = Convert.ToInt32(objCmd.ExecuteScalar()); if (intNumRows > 0) { this.pnlAdd.Visible = false; this.lblStatus.Visible = true; this.lblStatus.Text = "Username already exists."; } else { strSQL = "INSERT INTO member (Username,[Password],Name,Email) " + " VALUES " + " ('" + this.txtUsername.Text + "','" + this.txtPassword.Text + "', " + " '" + this.txtName.Text + "','" + this.txtEmail.Text + "')"; objCmd = new OleDbCommand(); { objCmd.Connection = objConn; objCmd.CommandText = strSQL; objCmd.CommandType = CommandType.Text; } this.pnlAdd.Visible = false; try { objCmd.ExecuteNonQuery(); this.lblStatus.Text = "Register Sucessful."; this.lblStatus.Visible = true; } catch (Exception ex) { this.lblStatus.Visible = true; this.lblStatus.Text = "Cannot Register : Error (" + ex.Message + ")"; } } objConn.Close(); objConn = null; } </script> <html> <head> <title>ThaiCreate.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:Panel id="pnlAdd" runat="server"> <asp:Label id="lblRegister" Text="Register New Member." runat="server" /> <br /> <br /> <table border="1" style="width: 387px"> <tbody> <tr> <td> <asp:Label id="lblUsername" runat="server" text="Username"></asp:Label></td> <td> <asp:TextBox id="txtUsername" runat="server" Width="120px"></asp:TextBox> <asp:RequiredFieldValidator ID="ChkUsername" runat="server" ControlToValidate="txtUsername" ErrorMessage="* Username"></asp:RequiredFieldValidator> </td> </tr> <tr> <td> <asp:Label id="lblPassword" runat="server" text="Password"></asp:Label></td> <td> <asp:TextBox id="txtPassword" runat="server" Width="100px" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label id="lblConPassword" runat="server" text="Confirm Password"></asp:Label></td> <td> <asp:TextBox id="txtConPassword" runat="server" Width="100px" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label id="lblName" runat="server" text="Name"></asp:Label></td> <td> <asp:TextBox id="txtName" runat="server" Width="199px"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label id="lblEmail" runat="server" text="Email"></asp:Label></td> <td> <asp:TextBox id="txtEmail" runat="server" Width="196px"></asp:TextBox> </td> </tr> </tbody> </table> <br /> <asp:Button id="btnSave" onclick="btnSave_Click" runat="server" Text="Save"></asp:Button> <br /> </asp:Panel> <asp:Label id="lblStatus" runat="server" visible="False"></asp:Label> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท