コード例 #1
0
ファイル: Signup.ascx.cs プロジェクト: chiuchiuJ/JUIC
        protected void Button2_Click(object sender, EventArgs e)
        {
            // 將報名結果匯入資料庫

            //判斷使用者是否已報名
            string strConn1 = ConfigurationManager.ConnectionStrings["MENU"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(strConn1))
            {
                String QQ = "select emp_no from TB_SignupTotal where eventID='" + LabelID.Text + "' and emp_no='" + LabeluserID.Text + "'";

                SqlDataAdapter da = new SqlDataAdapter(QQ, conn);
                DataSet        ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    Labelvalid.Text = ds.Tables[0].Rows[0][0].ToString().Trim();
                }
                ds.Clear();
            }

            if (Labelvalid.Text == LabeluserID.Text)
            {
                string errormessage = "你已經報名過囉!(修改報名資訊請洽資訊組)";

                string strJS1 = "alert('" + errormessage + "');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", strJS1, true);
                return;
                //因使用者已報名,跳出事件
            }



            //防呆,人數輸入錯誤跳出警告
            int  people;
            bool peopleresult = int.TryParse(TextBox3.Text.Trim(), out people);

            if (peopleresult == false || people < 1)
            {
                string errormessage2 = "請確認輸入正確人數";

                string strJS1 = "alert('" + errormessage2 + "');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", strJS1, true);
                TextBox3.Focus();

                return;
            }


            string strConn = ConfigurationManager.ConnectionStrings["MENU"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(strConn))
            {
                string strCmd = "Insert into TB_SignupTotal(eventID,title,emp_no,emp_cne,People,PS,DetailTable)values(@eventID,@title,@emp_no,@emp_cne,@People,@PS,@DetailTable)";
                using (SqlCommand cmd = new SqlCommand(strCmd, conn))
                {
                    cmd.Parameters.AddWithValue("@eventID", int.Parse(LabelID.Text));
                    cmd.Parameters.AddWithValue("@title", Label1.Text);
                    cmd.Parameters.AddWithValue("@emp_no", LabeluserID.Text);
                    cmd.Parameters.AddWithValue("@emp_cne", LabeluserName.Text);
                    cmd.Parameters.AddWithValue("@People", people);
                    cmd.Parameters.AddWithValue("@PS", TextBox2.Text);
                    cmd.Parameters.AddWithValue("@DetailTable", "1");

                    string message = "";

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        message = "報名成功";
                    }
                    catch (Exception ex)
                    {
                        message = ex.Message;
                    }
                    finally
                    {
                        conn.Close();
                        //string strJS = "alert('" + message + "');location.href = ('Join.aspx');";
                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "", strJS, true);
                    }
                }
            }
            //取得方才報名的序號
            JuicDao dao = new JuicDao("MENU");
            var     GetSingupTotalid = dao.GetSingupTotalid(LabelID.Text, LabeluserID.Text);
            int     SingupTotalid    = 0;

            if (GetSingupTotalid.Rows.Count != 0)
            {
                SingupTotalid = int.Parse(GetSingupTotalid.Rows[0]["ID"].ToString());
            }
            //建立空白同行者資料在TB_SignupTotalDetail
            string strConn2 = ConfigurationManager.ConnectionStrings["MENU"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(strConn2))
            {
                for (int i = 0; i < people; i++)
                {
                    //string strCmd = "Insert into TB_SignupTotalDetail(SignupTotalid,Name,IDnumber,Birthday,Old,Who,Bigbus,Eat,PS)values(@SignupTotalid,@Name,@IDnumber,@Birthday,@Old,@Who,@Bigbus,@Eat,@PS)";
                    string strCmd = "Insert into TB_SignupTotalDetail(SignupTotalid,Name)values(@SignupTotalid,@Name)";
                    using (SqlCommand cmd = new SqlCommand(strCmd, conn))
                    {
                        cmd.Parameters.AddWithValue("@SignupTotalid", SingupTotalid);
                        if (i == 0)
                        {
                            cmd.Parameters.AddWithValue("@Name", LabeluserName.Text);
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@Name", "");
                        }


                        string message = "";

                        try
                        {
                            conn.Open();
                            cmd.ExecuteNonQuery();
                            //message = "報名成功";
                        }
                        catch (Exception ex)
                        {
                            message = ex.Message;
                        }
                        finally
                        {
                            conn.Close();
                        }
                    }
                }
                string strJS = "alert('報名成功,請繼續填寫詳細資訊');location.href = ('Signupresult.aspx');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", strJS, true);
            }
        }