Exemple #1
0
        //Đăng ký tài khoản
        private void buttonDangKy_Click(object sender, EventArgs e)
        {
            SqlConnection con = cn.KetNoi();

            string     procName = "DangKyTaiKhoan";                                 //goi store DangKyTaiKhoan trong sql server
            SqlCommand cmd      = new SqlCommand(procName, con);

            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            //truyen tham so vao stored proc
            cmd.Parameters.AddWithValue("@ten", ten.Text);                                                  //du lieu nhap tu text box
            cmd.Parameters.AddWithValue("@ngaysinh", Convert.ToDateTime(dateTimePickerNgaySinh.Text));      //du lieu nhap tu text box

            if (checkBoxNam.Checked == true)                                                                //dữ liệu nhập từ checkbox
            {
                cmd.Parameters.AddWithValue("@gioitinh", 1);
            }
            if (checkBoxNu.Checked == true)
            {
                cmd.Parameters.AddWithValue("@gioitinh", 0);
            }

            cmd.Parameters.AddWithValue("@sdtNV", SDT.Text);        //du lieu nhap tu text box
            cmd.Parameters.AddWithValue("@email", email.Text);      //du lieu nhap tu text box
            cmd.Parameters.AddWithValue("@pass", pass.Text);        //du lieu nhap tu text box
            if (checkBoxKH.Checked == true)
            {
                cmd.Parameters.AddWithValue("@loaitk", 1);
                SDT.Enabled = false;
            }
            if (checkBoxNV.Checked == true)
            {
                cmd.Parameters.AddWithValue("@loaitk", 0);
                dateTimePickerNgaySinh.Enabled = false;
                checkBoxNam.Enabled            = false;
                checkBoxNu.Enabled             = false;
            }

            //get value form out put parameter in stored proc
            SqlParameter outputParameter = new SqlParameter();

            outputParameter.ParameterName = "@kq";
            outputParameter.SqlDbType     = System.Data.SqlDbType.Int;
            outputParameter.Direction     = System.Data.ParameterDirection.Output;
            cmd.Parameters.Add(outputParameter);
            //doan code tren de nhan gia tri tra ve tu sp

            con.Open();
            cmd.ExecuteNonQuery();
            int frag = int.Parse(outputParameter.Value.ToString());

            //kiểm tra đăng ký thành công
            if (frag == 1)
            {
                MessageBox.Show("Đăng ký tài khoản thành công!");

                //chuyen qua form TrangChu
                this.Hide();
                DangNhap f = new DangNhap();
                f.Show();
            }
            else
            {
                MessageBox.Show("Thông tin nhập vào không hợp lê!\nVui lòng nhập lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                con.Close();
            }
        }