Windows 7 Professional 64 Bit
 Internet Explorer 8
 IIS 7
 Oracle Database 11g Release 2 64 Bit
 Oracle 11g ODAC and Oracle Developer Tools for Visual Studio 11.2.0.1.2
 Visual Web Developer 2010 Express

■本登録処理 Prev Top Next
関連ページ:なし


前々回の続きです。今回は本登録処理を実装します。ですがその前にメールの内容を確認します。

GET送信にてユーザーIDの値を送信しますが、ユーザーIDの値が丸わかりのため本来割り当てられた以外のユーザーIDで本登録することも不可能ではないです。 これはまずいのでユーザーIDの値は暗号化することをお勧めします。


19.Step.19 本登録処理のコーディング

---Step02_0.htm---


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
  <!--   UIDパラメータには USER_MASTER の USERID 項目を設定すること -->
  <a href="Step02_1.aspx?UID=000001">Step02の仮ジャンプページ</a>
</body>
</html>

これは開発時に使用するダミーページです。運用時には使用しません。開発時はこのページを「スタートページに設定」してください。

---Step02_1.aspx---


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Step02_1.aspx.cs" Inherits="AccountSample.Step02.Step02_2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>本登録</title>
</head>
<body>
    <form id="form1" runat="server">

    <asp:Panel ID="Pnl_Error" runat="server">
        <font color="red">ユーザーIDが不正です。</font>
        <p></p>

        ユーザー登録を行う場合は、下記のリンクへジャンプしてください。
        <p></p>
        <a href="../Step01/Step01.aspx">仮登録へジャンプ</a>
    </asp:Panel>
 
    <asp:Panel ID="Pnl_OK" runat="server">
        本登録を行います。項目を入力して送信ボタンをクリックしてください。
        <br />
        <label style="color:Green" >★</label>がついている項目は、必須入力項目です。
        <p></p>

        <div style="color:red;">
           ※パスワードは入力ミスを防ぐためにダブルチェックを行います。同じ値を入力してください。<br />
        </div>
        <p></p>

        ユーザーID:<asp:Label ID="Lbl_UID" runat="server" Text=""></asp:Label>
        <p></p>

        メールアドレス:<asp:Label ID="Lbl_EMail" runat="server" Text=""></asp:Label>
        <p></p>
  
        <label style="color:Green" >★</label> パスワード1:<asp:TextBox ID="Txt_Password1" Width="100px" MaxLength="6" runat="server"></asp:TextBox> <asp:Label ID="Lbl_PasswordMsg" runat="server" Text=""></asp:Label>
        <p></p>

        <label style="color:Green" >★</label> パスワード2:<asp:TextBox ID="Txt_Password2" Width="100px" MaxLength="6" runat="server"></asp:TextBox>
        <p></p>

        <label style="color:Green" >★</label> 名前:<asp:TextBox ID="Txt_Name" Width="700px" MaxLength="50" runat="server"></asp:TextBox> <asp:Label ID="Lbl_NameMsg" runat="server" Text=""></asp:Label>
        <p></p>

        <label style="color:Green" >★</label> 住所:<asp:TextBox ID="Txt_Address" Width="700px" MaxLength="50" runat="server"></asp:TextBox> <asp:Label ID="Lbl_AddressMsg" runat="server" Text=""></asp:Label>
        <p></p>

        <label style="color:Green" >★</label> 性別:<asp:RadioButton ID="Radio_Man" GroupName="Sex" Text="男" runat="server"  />
                                                      <asp:RadioButton ID="Radio_Woman" GroupName="Sex" Text="女" runat="server"  />
                                                      <asp:Label ID="Lbl_SexMsg" runat="server" Text=""></asp:Label>
        <p></p>
 
        <label style="color:Green" >★</label> 生年月日:<asp:TextBox ID="Txt_BirthDay" Width="100px" MaxLength="8" runat="server"></asp:TextBox> <asp:Label ID="Lbl_BirthDayMsg" runat="server" Text=""></asp:Label>
        <p></p>

        フリーコメント:<asp:TextBox ID="Txt_FreeComment" Width="700px" Height="50px" MaxLength="100" TextMode="MultiLine" runat="server"></asp:TextBox> <asp:Label ID="Lbl_FreeCommentMsg" runat="server" Text=""></asp:Label>
        <p></p>

        <asp:Button ID="Cmd_Update" runat="server" Text="本登録" OnClick="Cmd_Update_Click" /> <asp:Label ID="Lbl_Msg" runat="server" Text=""></asp:Label>
        <p></p>
    </asp:Panel>
    </form>
</body>
</html>

本登録画面のレイアウトです。

---Step02_1.aspx.cs---


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Text;
using System.Configuration;

namespace AccountSample.Step02
{
    public partial class Step02_2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 本登録ボタンをクリックしたときも Page_Load が処理されるので、初回起動時のみ処理されるようにする
            if (IsPostBack == true) return;

            bool hr = false;

            OracleConnection connection = null;
            OracleCommand command;
            OracleDataReader reader = null;

            // パラメータチェック
            if (Request.QueryString["UID"] == null || Request.QueryString["UID"].Length == 0)
            {
                goto EXIT;
            }

            Int32 UserID = Int32.Parse(Request.QueryString["UID"]);

            // ユーザーID表示
            Lbl_UID.Text = string.Format("{0:D6}", UserID);

            try
            {
                // OracleConnectionを作成
                connection = new OracleConnection(ConfigurationManager.ConnectionStrings["Sample"].ToString());

                // データベース接続
                connection.Open();

                // OracleCommandを作成
                command = new OracleCommand();

                command.Connection = connection;

                // メールアドレス取得
                command.CommandText = "";
                command.CommandText += "SELECT";
                command.CommandText += " E_Mail";
                command.CommandText += "   FROM  USER_MASTER";
                command.CommandText += "   WHERE USERID=" + UserID.ToString();
                command.CommandText += "     AND DELETEDATE IS NULL ";

                // SELECT文を実行
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    // ユーザーIDに対応するメールを取得する
                    Lbl_EMail.Text = reader.GetString(reader.GetOrdinal("E_Mail"));
                    break;
                }

            }
            catch (Exception ex)
            {
                goto EXIT;
            }

            hr = true;

        EXIT:
            if (reader != null)
            {
                reader.Close();
                reader.Dispose();
                reader = null;
            }

            if (connection != null)
            {
                connection.Close();
                connection.Dispose();
                connection = null;
            }

            if (hr == false)
            {
                // 不正なパラメータのため、エラーメッセージを表示する
                Pnl_OK.Visible = false;
                Pnl_Error.Visible = true;
            }

            else
            {
                Pnl_Error.Visible = false;
                Pnl_OK.Visible = true;
            }
        }

        // 本登録ボタンをクリックした
        protected void Cmd_Update_Click(object sender, EventArgs e)
        {
            Lbl_PasswordMsg.Text = "";
            Lbl_NameMsg.Text = "";
            Lbl_AddressMsg.Text = "";
            Lbl_SexMsg.Text = "";
            Lbl_BirthDayMsg.Text = "";
            Lbl_FreeCommentMsg.Text = "";
            Lbl_Msg.Text = "";

            OracleConnection connection = null;
            OracleCommand command;

            Int32 UserID = Int32.Parse(Lbl_UID.Text);

            // スペースを取り除く
            string Pass1 = Txt_Password1.Text.Trim();
            string Pass2 = Txt_Password2.Text.Trim();
            string Name = Txt_Name.Text.Trim();
            string Address = Txt_Address.Text.Trim();
            string Sex = "";
            if (Radio_Man.Checked == true)
                Sex = Radio_Man.Text;
            else if (Radio_Woman.Checked == true)
                Sex = Radio_Woman.Text;

            string BirthDay = Txt_BirthDay.Text.Trim();
            string FreeComment = Txt_FreeComment.Text.Trim();

            // 入力内容のチェック

            // パスワードに同じ値を入力しているかをチェック
            if (Pass1.Equals(Pass2) == false)
            {
                Lbl_PasswordMsg.Text = "同じパスワードを入力してください。";
                Lbl_PasswordMsg.ForeColor = System.Drawing.Color.Red;
                Txt_Password1.Focus();
                goto EXIT;
            }

            // 全角文字を使用しているかチェック
            if (Encoding.GetEncoding("Shift_JIS").GetBytes(Pass1).Length != Pass1.Length)
            {
                Lbl_PasswordMsg.Text = "半角文字だけで入力してください。";
                Lbl_PasswordMsg.ForeColor = System.Drawing.Color.Red;
                Txt_Password1.Focus();
                goto EXIT;
            }

            // 入力桁数のチェック
            if (Pass1.Length != 6)
            {
                Lbl_PasswordMsg.Text = "パスワードは6桁入力してください。";
                Lbl_PasswordMsg.ForeColor = System.Drawing.Color.Red;
                Txt_Password1.Focus();
                goto EXIT;
            }

            // 入力した文字の形式をチェック、とりあえず 数字:2文字、文字:2文字、記号:2文字 の仕様 
            int Suuji = 0;
            int Moji = 0;
            int Kigou = 0;
            for (int i = 0; i < Pass1.Length; i++)
            {
                string s = Pass1.Substring(i, 1);
                byte[] c = Encoding.GetEncoding("Shift_JIS").GetBytes(s);
                int result;

                if (int.TryParse(s, out result) == true)
                    Suuji++;
                else if (c[0] >= 'A' && c[0] <= 'Z' || c[0] >= 'a' && c[0] <= 'z')
                    Moji++;
                else if (c[0] >= '!' && c[0] <= '~')
                    Kigou++;
            }
            if (Suuji != 2 || Moji != 2 || Kigou != 2)
            {
                Lbl_PasswordMsg.Text = "数字、文字、記号をそれぞれ2文字づつ使用してください。";
                Lbl_PasswordMsg.ForeColor = System.Drawing.Color.Red;
                Txt_Password1.Focus();
                goto EXIT;
            }

            // 名前を入力しているか
            if (Name.Length == 0)
            {
                Lbl_NameMsg.Text = "名前を入力してください。";
                Lbl_NameMsg.ForeColor = System.Drawing.Color.Red;
                Txt_Name.Focus();
                goto EXIT;
            }

            // 住所を入力しているか
            if (Address.Length == 0)
            {
                Lbl_AddressMsg.Text = "住所を入力してください。";
                Lbl_AddressMsg.ForeColor = System.Drawing.Color.Red;
                Txt_Address.Focus();
                goto EXIT;
            }

            // 性別を入力しているか
            if (Sex.Length == 0)
            {
                Lbl_SexMsg.Text = "性別を入力してください。";
                Lbl_SexMsg.ForeColor = System.Drawing.Color.Red;
                goto EXIT;
            }

            // 生年月日を入力しているか
            if (BirthDay.Length != 8)
            {
                Lbl_BirthDayMsg.Text = "生年月日は8桁入力してください。";
                Lbl_BirthDayMsg.ForeColor = System.Drawing.Color.Red;
                Txt_BirthDay.Focus();
                goto EXIT;
            }

            // 全角文字を使用しているかチェック
            if (Encoding.GetEncoding("Shift_JIS").GetBytes(BirthDay).Length != BirthDay.Length)
            {
                Lbl_BirthDayMsg.Text = "半角文字だけで入力してください。";
                Lbl_BirthDayMsg.ForeColor = System.Drawing.Color.Red;
                Txt_BirthDay.Focus();
                goto EXIT;
            }

            // 有効な日付かチェック
            DateTime resultD;
            string BirthDayFormat = BirthDay.Substring(0, 4) + "/" + BirthDay.Substring(4, 2) + "/" + BirthDay.Substring(6, 2);
            if (DateTime.TryParse(BirthDayFormat, out resultD) == false)
            {
                Lbl_BirthDayMsg.Text = "有効な日付を入力してください。";
                Lbl_BirthDayMsg.ForeColor = System.Drawing.Color.Red;
                Txt_BirthDay.Focus();
                goto EXIT;
            }

            // フリーコメントの文字数をチェック
            // MultiLine を設定すると MaxLength プロパティで制限されないようになるのでここでチェックする
            if (Txt_FreeComment.Text.Length > 100)
            {
                Lbl_FreeCommentMsg.Text = "フリーコメントは100文字までで入力してください。";
                Lbl_FreeCommentMsg.ForeColor = System.Drawing.Color.Red;
                Txt_FreeComment.Focus();
                goto EXIT;
            }

            // 影響を受けた行数
            int UpdateCnt = 0;

            try
            {
                // OracleConnectionを作成
                connection = new OracleConnection(ConfigurationManager.ConnectionStrings["Sample"].ToString());

                // データベース接続
                connection.Open();

                // OracleCommandを作成
                command = new OracleCommand();

                command.Connection = connection;

                // 追加登録処理
                command.CommandText = "";
                command.CommandText += "UPDATE USER_MASTER SET";
                command.CommandText += "   PASSWORD='" + Pass1.Replace("'", "''") + "',";
                command.CommandText += "   NAME='" + Name.Replace("'", "''") + "',";
                command.CommandText += "   ADDRESS='" + Address.Replace("'", "''") + "',";
                command.CommandText += "   SEX='" + Sex + "',";
                command.CommandText += "   BIRTHDAY='" + BirthDay + "',";
                command.CommandText += "   FREECOMMENT='" + FreeComment.Replace("'", "''") + "',";
                command.CommandText += "   UPDATEDATE=SYSDATE ";
                command.CommandText += "   WHERE USERID=" + UserID.ToString();
                command.CommandText += "     AND DELETEDATE IS NULL ";

                UpdateCnt = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Lbl_Msg.Text = "登録に失敗しました。";
                Lbl_Msg.ForeColor = System.Drawing.Color.Red;
                goto EXIT;
            }

            if (UpdateCnt != 0)
            {
                Lbl_Msg.Text = "登録しました。";
                Lbl_Msg.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {
                Lbl_Msg.Text = "登録対象のデータが見つかりません。";
                Lbl_Msg.ForeColor = System.Drawing.Color.Red;
            }

        EXIT:

            if (connection != null)
            {
                connection.Close();
                connection.Dispose();
                connection = null;
            }
        }
    }
}

そういえばSQLコマンドについてはまったく説明してないですけど、わからない方は他サイトや書籍に情報がいっぱい転がってますので、そちらで調べてください。 申し訳ないけど。


Prev Top Next
inserted by FC2 system