Button to enter Records in Database

Asked

Viewed 30 times

0

Good, I am conducting a project for my PAP that consists in creating a Database for a company. However, I decided to put a button to add records and when I click to add, the following error appears: Failed to convert the value of the Bitmap parameter to Byte[]. I think it’s related to transferring the selected Image in the Form to the Database, and I don’t know how to resolve it. I leave you the BD code; Button to insert the Image in the Picturebox; Button to insert the records and also the image of the same error:

Database

    CREATE TABLE [dbo].[Utilizador] (
        [IDuser]           INT          NOT NULL,
        [Nome]             VARCHAR (50) NOT NULL,
        [DataDeNascimento] DATE         NOT NULL,
        [Idade]            VARCHAR (3)  NOT NULL,
        [Email]            VARCHAR (50) NULL,
        [Telefone]         CHAR (9)     NULL,
        [TelefoneEE]       CHAR (9)     NULL,
        [Imagem]           IMAGE        NULL,
        [Login]            VARCHAR (50) NOT NULL,
        [Senha]            VARCHAR (50) NOT NULL,
        [Perfil]           VARCHAR (20) NOT NULL,
        [Localidade]       VARCHAR (50) NOT NULL,
        PRIMARY KEY CLUSTERED ([IDuser] ASC)
);

Button Code to Insert Image

string Chosen_File = "";
            openFD.InitialDirectory = "C:";
            openFD.Title = "Inserir uma Imagem";
            openFD.FileName = "";
            openFD.Filter = "JPEG Images|*.jpg|GIF Images|*.gif";
            if (openFD.ShowDialog() == DialogResult.Cancel)
            {
                MessageBox.Show("Operation Cancelled");
            }
            else
            {
                Chosen_File = openFD.FileName;
                imagemPictureBox.Image = Image.FromFile(Chosen_File);
            }

Button Code to Add Logs

SqlConnection sql = new SqlConnection("Data Source=ocaminhodabasededados");
            SqlCommand command = new SqlCommand("insert into Utilizador(IDuser, Nome, Localidade, DataDeNascimento, Idade, Email, Login, Senha, Telefone, TelefoneEE, Perfil, Imagem) 
                             values (@IDuser, @Nome, @Localidade, @DatadeNascimento, @Idade, @Email, @Login, @Senha, @Telefone, @TelefoneEE, @Perfil, @Imagem", sql);
            command.Parameters.Add("@IDuser", SqlDbType.VarChar).Value = iDuserTextBox.Text;
            command.Parameters.Add("@Nome", SqlDbType.VarChar).Value = nomeTextBox.Text;
            command.Parameters.Add("@Localidade", SqlDbType.VarChar).Value = localidadeTextBox.Text;
            command.Parameters.Add("@DataDeNascimento", SqlDbType.DateTime).Value = dataDeNascimentoDateTimePicker.Value;
            command.Parameters.Add("@Idade", SqlDbType.VarChar).Value = idadeTextBox.Text;
            command.Parameters.Add("@Email", SqlDbType.VarChar).Value = emailTextBox.Text;
            command.Parameters.Add("@Login", SqlDbType.VarChar).Value = loginTextBox.Text;
            command.Parameters.Add("@Senha", SqlDbType.VarChar).Value = senhaTextBox.Text;
            command.Parameters.Add("@Telefone", SqlDbType.Char).Value = telefoneTextBox.Text;
            command.Parameters.Add("@TelefoneEE", SqlDbType.Char).Value = telefoneEETextBox.Text;
            command.Parameters.Add("@Imagem", SqlDbType.Image).Value = imagemPictureBox.Image;
            command.Parameters.Add("@Perfil", SqlDbType.VarChar).Value = perfilTextBox.Text;

Bug Image inserir a descrição da imagem aqui

1 answer

0

You are not converting the component attribute to the expected type. You can use the class ImageConverter for that reason.

var imageBytes = new ImageConverter().ConvertTo(imagemPictureBox.Image, typeof(byte[]));
command.Parameters.Add("@Imagem", SqlDbType.Image).Value = imageBytes;

Browser other questions tagged

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