Beispiel #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();

            con = new SqlConnection("Data Source=GEORGEDESK\\SQLEXPRESS;Initial Catalog=PARCIALJP;Integrated Security=True");
            con.Open();

            if (txtusername.Text != "" && txtpassword.Text != "" && txtpassword2.Text != "" && txtnombre.Text != "")
            {
                if (txtpassword2.Text != txtpassword.Text)
                {
                    MessageBox.Show("Las Contraseñas no coinciden");
                }
                else
                {
                    string username = Helper.GetSha3Hash((txtusername.Text).ToString());
                    string password = Helper.GetSha3Hash((txtpassword.Text).ToString());
                    try
                    {
                        string     query = $"SELECT COUNT(*) FROM USUARIO WHERE USERNAME = '******'";
                        SqlCommand cmd   = new SqlCommand(query, con);
                        Int32      count = Convert.ToInt32(cmd.ExecuteScalar());
                        if (count == 0)
                        {
                            try
                            {
                                string sql = $"INSERT INTO USUARIO VALUES ('{username}', '{password}', @tipo, @estado, @nombre)";

                                SqlCommand command = new SqlCommand(sql);
                                command.Parameters.AddWithValue("@nombre", txtnombre.Text);
                                command.Parameters.AddWithValue("@tipo", cbxtipo.SelectedValue.ToString());
                                command.Parameters.AddWithValue("@estado", cbxestado.SelectedValue.ToString());
                                Helper.DoQueryExecuterLimpio(command);
                                if (Program.tipo != "")
                                {
                                    FrmUsuario fmusuario = new FrmUsuario();
                                    MessageBox.Show("Usuario registrado con exito ");
                                    this.Close();
                                }
                                else
                                {
                                    MessageBox.Show("Bienvenido " + txtusername.Text);
                                    FrmMenu fmMenu = new FrmMenu();
                                    fmMenu.Show();
                                    this.Hide();
                                }
                            }
                            catch (Exception er)
                            {
                                MessageBox.Show("No se pudo registrar " + er);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Usuario no Disponible");
                        }
                    }
                    catch (Exception er)
                    {
                        MessageBox.Show("No se pudo ingresar al sistema " + er);
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
            else
            {
                MessageBox.Show("Datos incompletos");
            }
        }