Example #1
0
    protected void InsertButton_Click(object sender, EventArgs e)
    {
        TextBox txtBoxPwd = ((TextBox)(FormView2.FindControl("ClaveTextBox")));
        TextBox txtBoxCod = ((TextBox)(FormView2.FindControl("CodigoTextBox")));
        TextBox txtBoxNom = ((TextBox)(FormView2.FindControl("NombreTextBox")));
        DropDownList ddlPerfil = ((DropDownList)(FormView2.FindControl("cboPerfilUsuario")));
        DropDownList ddlPais = ((DropDownList)(FormView2.FindControl("cboPaisUsuario")));
        CheckBox chkboxEstado = ((CheckBox)(FormView2.FindControl("EstadoCheckBox")));

        ConexionDatos cd = new ConexionDatos();
        SqlConnection cn = new SqlConnection(cd.getConnectionString());
        SqlCommand cmd = new SqlCommand();

        try
        {
            cn.Open();

            cmd = new SqlCommand("INSERT INTO Usuario VALUES (@perfilID, @paisID, @usuario, @clave, @nombre, @estado)", cn);
            cmd.Parameters.Add("@perfilID", SqlDbType.Int).Value = ddlPerfil.SelectedValue;
            cmd.Parameters.Add("@paisID", SqlDbType.Int).Value = ddlPais.SelectedValue;
            cmd.Parameters.Add("@nombre", SqlDbType.VarChar, 50).Value = txtBoxCod.Text;
            cmd.Parameters.Add("@usuario", SqlDbType.VarChar, 15).Value = txtBoxNom.Text;
            cmd.Parameters.Add("@clave", SqlDbType.VarChar, 15).Value = txtBoxPwd.Text;
            cmd.Parameters.Add("@estado", SqlDbType.Bit).Value = chkboxEstado.Checked;

            cmd.ExecuteNonQuery();
            cmd.Transaction.Commit();
        }
        catch (Exception ex)
        {
            //System.Windows.Forms.MessageBox.Show(ex.Message);
            cmd.Transaction.Rollback();
        }
        finally
        {
            cn.Close();
        }
    }