Topic: SG: An add_user page to study for you Steve  (Read 1382 times)

0 Members and 1 Guest are viewing this topic.

Offline toasty0

  • Application.Quit();
  • Captain
  • *
  • Posts: 8045
  • Gender: Male
SG: An add_user page to study for you Steve
« on: June 04, 2005, 11:09:32 pm »

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Page language="c#" Codebehind="AddUser.aspx.cs" AutoEventWireup="false" Inherits="PW_Test.AddUser" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
   <HEAD>
      <title>AddUser</title>
      <script runat="server">
    private void Page_load(Object Src, EventArgs e)
    {
   
    String email = Request.QueryString["UserEmail"];
   
    if(null != email)
    UserEmail.Value=email;
    }
   
    private void AddUser_Click(Object sender, EventArgs e)
    {
    if( !Page.IsValid)
    {
    Msg.Text="Some required fields are invalid.";
    return;
    }
   
    DataSet ds=new DataSet();
   
    String userFile= ".../users.xml";
   
    FileStream fs = new FileStream(Server.MapPath(userFile), FileMode.Open, FileAccess.Read);
    StreamReader reader= new StreamReader(fs);
    ds.ReadXml(reader);
    fs.Close();
   
    string hashedpwd=FormsAuthentication.HashPasswordForStoringInConfigFile(UserPass.Value, "SHA1");
    DataRow newUser=ds.Tables[0].NewRow();
    newUser["UserEmail"]=UserEmail.Value;
    newUser["UserPassword"]=hashedpwd;
    ds.Tables[0].Rows.Add(newUser);
    ds.AcceptChanges();
   
    fs= new FileStream(Server.MapPath(userFile),FileMode.Create,
    FileAccess.Write|FileAccess.Read);
    StreamWriter writer = new StreamWriter(fs);
    ds.WriteXml(writer);
    writer.Close();
    fs.Close();
   
    Response.Redirect(".../Default.aspx");
    }
      </script>
      <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
      <meta name="CODE_LANGUAGE" Content="C#">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
   </HEAD>
   <body MS_POSITIONING="GridLayout">
      <form id="Form1" method="post" runat="server">
         <div style="BACKGROUND:#ccccff">
            <h3><font face="Verdana">Add New User</font></h3>
         </div>
         <table>
            <tr>
               <td>Name</td>
               <td><input id="UserEmail" type="text" runat="server"></td>
               <td><asp:RequiredFieldValidator ControlToValidate="UserEmail" Display="Static" ErrorMessage="*" runat="server" id="RequiredFieldValidator1" />
               </td>
               <td>
                  <asp:RegularExpressionValidator ID="RegexValidator" ControlToValidate="UserEmail" ValidateExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-)+\.)+))&#13;&#10;&#9;&#9;&#9;&#9;&#9;([a-zA-Z](2,4)|[0-9]{1,3})(\]?)$"
                     EnableClientScript="False" Display="Static" ErrorMessage="*" runat="server" />
               </td>
            </tr>
            <tr>
               <td>Password</td>
               <td><input id="UserPass" type="password" runat="server"></td>
               <td><asp:RequiredFieldValidator ControlToValidate="UserPass" Display="Static" ErrorMessage="*" Runat="server" id="RequiredFieldValidator2" /></td>
            </tr>
            <tr>
               <td>Persistant Forms</td>
               <td><asp:CheckBox ID="persist" Runat="server" AutoPostBack="True" /></td>
            </tr>
         </table>
         <input type="submit" onserverclick="AddUser_Click" value="Add User" runat="server"><p>
            <asp:Label ID="Msg" ForeColor="red" Font-Name="Verdana" Font-Size="10" Runat="server" />
      </form>
      </P>
   </body>
</HTML>
MCTS: SQL Server 2005 | MCP: Windows Server 2003 | MCTS: Microsoft Certified Technology Specialist | MCT: Microsoft Certified Trainer | MOS: Microsoft Office Specialist 2003 | VSP: VMware Sales Professional | MCTS: Vista

Offline Sirgod

  • Whooot Master Cattle Baron
  • Global Moderator
  • Vice Admiral
  • *
  • Posts: 27844
  • Gender: Male
Re: SG: An add_user page to study for you Steve
« Reply #1 on: June 05, 2005, 12:39:18 am »
Wow that's alot of code for just adding a user to a web Page/Forum. Is there an easier way to do this? Or is finding out part of this weeks leason Plan?  ;)

I have got to get my head back into this. I can somewhat follow the stuff now, But i could never right anything off the top of my head like  you and others here can.

Stephen
"You cannot exaggerate about the Marines. They are convinced to the point of arrogance, that they are the most ferocious fighters on earth - and the amusing thing about it is that they are."- Father Kevin Keaney, Chaplain, Korean War

Offline toasty0

  • Application.Quit();
  • Captain
  • *
  • Posts: 8045
  • Gender: Male
Re: SG: An add_user page to study for you Steve
« Reply #2 on: June 05, 2005, 09:52:29 am »
Some of that code is for page/control rendering .

The following is the code that adds the user:

<script runat="server">
    private void Page_load(Object Src, EventArgs e)
    {
   
    String email = Request.QueryString["UserEmail"];
   
    if(null != email)
    UserEmail.Value=email;
    }
   
    private void AddUser_Click(Object sender, EventArgs e)
    {
    if( !Page.IsValid)
    {
    Msg.Text="Some required fields are invalid.";
    return;
    }
   
    DataSet ds=new DataSet();
   
    String userFile= ".../users.xml";
   
    FileStream fs = new FileStream(Server.MapPath(userFile), FileMode.Open, FileAccess.Read);
    StreamReader reader= new StreamReader(fs);
    ds.ReadXml(reader);
    fs.Close();
   
    string hashedpwd=FormsAuthentication.HashPasswordForStoringInConfigFile(UserPass.Value, "SHA1");
    DataRow newUser=ds.Tables[0].NewRow();
    newUser["UserEmail"]=UserEmail.Value;
    newUser["UserPassword"]=hashedpwd;
    ds.Tables[0].Rows.Add(newUser);
    ds.AcceptChanges();
   
    fs= new FileStream(Server.MapPath(userFile),FileMode.Create,
    FileAccess.Write|FileAccess.Read);
    StreamWriter writer = new StreamWriter(fs);
    ds.WriteXml(writer);
    writer.Close();
    fs.Close();
   
    Response.Redirect(".../Default.aspx");
    }
      </script>


The following is the HTML that is rendered on the client machine by their browser except where you see the directive 'runat=server'.

 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
      <meta name="CODE_LANGUAGE" Content="C#">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
   </HEAD>
   <body MS_POSITIONING="GridLayout">
      <form id="Form1" method="post" runat="server">
         <div style="BACKGROUND:#ccccff">
            <h3><font face="Verdana">Add New User</font></h3>
         </div>
         <table>
            <tr>
               <td>Name</td>
               <td><input id="UserEmail" type="text" runat="server"></td>
               <td><asp:RequiredFieldValidator ControlToValidate="UserEmail" Display="Static" ErrorMessage="*" runat="server" id="RequiredFieldValidator1" />
               </td>
               <td>
                  <asp:RegularExpressionValidator ID="RegexValidator" ControlToValidate="UserEmail" ValidateExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-)+\.)+))&#13;&#10;&#9;&#9;&#9;&#9;&#9;([a-zA-Z](2,4)|[0-9]{1,3})(\]?)$"
                     EnableClientScript="False" Display="Static" ErrorMessage="*" runat="server" />
               </td>
            </tr>
            <tr>
               <td>Password</td>
               <td><input id="UserPass" type="password" runat="server"></td>
               <td><asp:RequiredFieldValidator ControlToValidate="UserPass" Display="Static" ErrorMessage="*" Runat="server" id="RequiredFieldValidator2" /></td>
            </tr>
            <tr>
               <td>Persistant Forms</td>
               <td><asp:CheckBox ID="persist" Runat="server" AutoPostBack="True" /></td>
            </tr>
         </table>
         <input type="submit" onserverclick="AddUser_Click" value="Add User" runat="server"><p>
            <asp:Label ID="Msg" ForeColor="red" Font-Name="Verdana" Font-Size="10" Runat="server" />
      </form>
      </P>
   </body>
</HTML>
MCTS: SQL Server 2005 | MCP: Windows Server 2003 | MCTS: Microsoft Certified Technology Specialist | MCT: Microsoft Certified Trainer | MOS: Microsoft Office Specialist 2003 | VSP: VMware Sales Professional | MCTS: Vista