Image upload using Picturebox - windows Forms

Asked

Viewed 97 times

0

This is Galera, good morning! I have a small problem and I can’t solve it, it’s the first time I’m using c#Picturebox, my application is Windows Forms. and I’m making a record using photo! but when I am inserting the data and converting the photo to binary in my database my execution returns me this error. inserir a descrição da imagem aqui

this is my Insert code:

private void BT_SaveOnibus_Click(object sender, EventArgs e)
    {
        try
        {
            if (TB_NomeUser.Text == "")
            {
                MessageBox.Show("Informe o nome do Usuário", "Aviso do sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TB_NomeUser.Focus();
                return;

            }
            if (MK_TelUser.Text == "")
            {
                MessageBox.Show("Informe o Telefone", "Aviso do sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                MK_TelUser.Focus();
                return;

            }
            if (TB_RgUser.Text == "")
            {
                MessageBox.Show("Informe o Rg do Usuário", "Aviso do sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TB_RgUser.Focus();
                return;
            }
            if (TB_CpfUser.Text == "")
            {
                MessageBox.Show("Informe o Cpf do Usuário", "Aviso do sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TB_CpfUser.Focus();
                return;
            }

            if (CLB_Opcao.CheckedItems.Count > 0)
            {
                for (int i = 0; i < CLB_Opcao.CheckedItems.Count; i++)
                {
                    if (str == "")
                    {
                        str = CLB_Opcao.CheckedItems[i].ToString();
                    }
                    else
                    {
                        str += "," + CLB_Opcao.CheckedItems[i].ToString();
                    }
                }
            }
            cg.con = new SqlConnection(cn.DBconn);
            cg.con.Open();
            string cb = "insert into CadOnibusUser(F_Cod,Id_UserOnibus,TB_NomeUser,TB_TelefoneUser,TB_RgUser,TB_CpfUser,TB_OpcaoUser,TB_ImageUser,TB_NomePropri,TB_TelefonePropri,TB_EnderecoPropri,TB_EnderecoPropri,TB_QuadraPropri,TB_EmailPropri,TB_RgPropri,TB_CpfPropri,TB_NumeroPropri,TB_LotePropri) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17)";
            cg.cmd = new SqlCommand(cb);
            cg.cmd.Connection = cg.con;
            cg.cmd.Parameters.AddWithValue("@d1", TB_IdFicha.Text);
            cg.cmd.Parameters.AddWithValue("@d2", TB_id.Text);
            cg.cmd.Parameters.AddWithValue("@d3", TB_NomeUser.Text);
            cg.cmd.Parameters.AddWithValue("@d4", MK_TelUser.Text);
            cg.cmd.Parameters.AddWithValue("@d5", TB_RgUser.Text);
            cg.cmd.Parameters.AddWithValue("@d6", TB_CpfUser.Text);
            cg.cmd.Parameters.AddWithValue("@d7", str);

            cg.cmd.Parameters.AddWithValue("@d9", TB_NomeProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d10", TB_NumeroProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d11", TB_EnderecoProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d12", TB_QdProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d13", TB_EmailProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d14", TB_RgProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d15", TB_CpfProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d16", TB_NumeroProprietario.Text);
            cg.cmd.Parameters.AddWithValue("@d17", TB_LoteProprietario.Text);
            converterFoto();




            cg.cmd.ExecuteReader();
            cg.con.Close();

            st1 = Lbl_Usuario.Text;
            st2 = "Novo Usuário incluído '" + TB_NomeUser.Text + "' com seguinte id '" + TB_id.Text + "'";
            cf.LogFunc(st1, System.DateTime.Now, st2);

            BT_SaveOnibus.Enabled = false;

            MessageBox.Show("Salvo com sucesso", "Registro", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        catch (Exception erro)
        {
            MessageBox.Show(erro.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

This is my way

void converterFoto()
    {
        //convertendo a foto para dados binários
        if (picFoto.Image != null)
        {
            ms = new MemoryStream();
            picFoto.Image.Save(ms, ImageFormat.Jpeg);
            byte[] foto_array = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(foto_array, 0, foto_array.Length);
            cg.cmd.Parameters.AddWithValue("@d8", foto_array);
        }
    }

NOTE: I am using DB sqlServer- and the image column type I set to "image" From now on I thank Galera!

  • This Exception refers to your query. The Tb_addressee column and the parameter quantity is different.

  • @Netinhosantos, I had not noticed that it was duplicated, I will correct and do the tests again!

  • @Netinhosantos already corrected my code and worked Thanks for the remark!

1 answer

0

The problem is occurring because you have a different amount of columns and values in Insert.

Note that you have 18 columns, but only 17 values.

insert into CadOnibusUser
(
F_Cod,
Id_UserOnibus,
TB_NomeUser,
TB_TelefoneUser,
TB_RgUser,
TB_CpfUser,
TB_OpcaoUser,
TB_ImageUser,
TB_NomePropri,
TB_TelefonePropri,
TB_EnderecoPropri,
TB_EnderecoPropri,
TB_QuadraPropri,
TB_EmailPropri,
TB_RgPropri,
TB_CpfPropri,
TB_NumeroPropri,
TB_LotePropri
)

VALUES 
(
@d1,
@d2,
@d3,
@d4,
@d5,
@d6,
@d7,
@d8,
@d9,
@d10,
@d11,
@d12,
@d13,
@d14,
@d15,
@d16,
@d17
)

To solve the problem enter the parameter @D18 that will work normally.

  • No comentario acima eu já Percebido @Pedro Paulo! já feito a correction do código e funciona ! obrigado de qual quer forma Abraços!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.