Image Upload Using PictureBox - windows forms

0

Say hello, good morning! I'm not sure how to do this, but I'm not sure how to do this. and I'm making a photo using photo! however when doing the insertion of the data and converting the photo to binary in my database my statement returns this error.

thisismyinsertcode:

privatevoidBT_SaveOnibus_Click(objectsender,EventArgse){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 method

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 defined as "image" Thanks in advance!

    
asked by anonymous 03.07.2018 / 14:45

1 answer

1

The problem is occurring because you have a different amount of columns and values in the 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 @ d18 parameter that will work normally.

    
03.07.2018 / 15:15