0

When performing a query in the database, and passing this information to an instance of WindowsForms, the following error occurs:

enter image description here

Here is the execution code:

    private void msi_editar_Click(object sender, EventArgs e)
    {
        try
        {
        string codusu = dgv_usuarios.SelectedRows[0].Cells[0].Value.ToString();
        dt = Connection.select($@"select * from view_coa_cadusu where codusu = {codusu}");
        this.Hide();
        ManipulaUsuario mu = new ManipulaUsuario(
            dt.Rows[0].Field<String>("coa_nomcomp"),
            dt.Rows[0].Field<String>("nomusu"),
            dt.Rows[0].Field<String>("senusu"),
            dt.Rows[0].Field<String>("coa_emailusu"),
            dt.Rows[0].Field<Char>("ativo"),
            dt.Rows[0].Field<String>("coa_telusu"),
            dt.Rows[0].Field<String>("coa_endusu"),
            dt.Rows[0].Field<String>("coa_numendusu"),
            dt.Rows[0].Field<String>("coa_baiendusu"),
            dt.Rows[0].Field<String>("coa_complendusu"),
            dt.Rows[0].Field<String>("coa_cependusu")
            );

        mu.FormClosed += (s, args) => this.Show();
        mu.Text = "Edição de usuário";
        mu.ShowDialog();
        } catch (ArgumentOutOfRangeException ex)
        {
            MessageBox.Show("Selecione um usuário válido", "Erro de seleção");
            return;
        }
    }

Here's the query view I'm using in the database:

SELECT cadusu.codusu,
cadusu.nomusu,
cadusu.senusu,
COALESCE(cadusu.nomcompusu, ''::character varying) AS coa_nomcomp,
cadusu.ativo,
COALESCE(cadusu.emailusu, ''::character varying) AS coa_emailusu,
COALESCE(cadusu.telusu, ''::character varying) AS coa_telusu,
COALESCE(cadusu.endusu, ''::character varying) AS coa_endusu,
COALESCE(cadusu.numendusu, ''::character varying) AS coa_numenusu,
COALESCE(cadusu.baiendusu, ''::character varying) AS coa_baiendusu,
COALESCE(cadusu.complendusu, ''::character varying) AS coa_complendusu,
COALESCE(cadusu.cependusu, ''::character varying) AS coa_cependusu

FROM cadusu;

Here is the code for the Windows Forms instance being called:

    public ManipulaUsuario()
    {
        InitializeComponent();
    }
    public ManipulaUsuario(string nomcompusu, string nomusu, string senusu, string emailusu, char ativo, string telusu, string endusu, string numendusu, string baiendusu, string complendusu, string cependusu) : this()
    {
        lb_ativo.Visible = true;
        rb_ativo.Visible = true;
        rb_inativo.Visible = true;

        tb_nome.Text = nomcompusu;
        tb_usuario.Text = nomusu;
        tb_senha.Text = senusu;
        tb_email.Text = emailusu;
        tb_telefone.Text = telusu;
        tb_endereco.Text = endusu;
        tb_numero_endereco.Text = numendusu;
        tb_bairro.Text = baiendusu;
        tb_complemento.Text = complendusu;
        tb_cep.Text = cependusu;
        if (ativo == 'S')
        {
            rb_ativo.Checked = true;
        } else
        {
            rb_inativo.Checked = true;
        }
    }

After the query, the data from the database has to go to the screen of the other forms, I've tried to put .ToString() at the end of each selection, but without success.

I wanted to know why it's not working and how to fix it.

5
  • What is the data type of column ativo ? Thats causing it Commented Jun 17, 2022 at 17:30
  • The data type is char Commented Jun 17, 2022 at 17:31
  • Use the CASE expression to return 'S' or 'N' from your sql query Commented Jun 17, 2022 at 17:32
  • Return S or N as string instead of char? Commented Jun 17, 2022 at 17:35
  • Are you using Npgsql ? What does connection.Select do, it looks strange , npgsql uses a NpgsqlCommand object and ExecuteReader to get records from a SELECT query. Any of the columns have null value? Try to remove one column after another from your query to find which column is giving you the cast Exception. Commented Jun 18, 2022 at 2:37

1 Answer 1

0

Your data types must be exactly the same as the data types in the database to avoid clr error.

Of course, you can also use linq or lambda for it

var row = dt.AsEnumerable().FirstOrDefault();
ManipulaUsuario mu = new ManipulaUsuario(
            row.Field<String>("coa_nomcomp"),
            row.Field<String>("nomusu"),
            row.Field<String>("senusu"),
            .......
            );
Sign up to request clarification or add additional context in comments.

2 Comments

Ended up giving the same error
The error is due to data types. Check them. Data types must be the same, for example bigint must be long

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.