Button to enter Records in Database

Asked

Viewed 39 times

0

Good, I am conducting a project for my PEF 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[]. What should I do? I leave you the following image that contains the error and send the code here:

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;

inserir a descrição da imagem aqui

1 answer

1

Try to manually convert the image to byte before sending it to the bank

ImageConverter converter = new ImageConverter();
imagem = (byte[])converter.ConvertTo(img, typeof(byte[]));
  • Good, I put this code before private void btnAionar_Click? Or inside it?

  • It can be inside even, depends on how you ta structuring your project

  • Good, I did what you suggested but gave error in Imageconverter converter = new Imageconverter(); image = (byte[])convert.Convertto(img, typeof(byte[]); instead of meti image Imagempicturebox but at img I don’t know what to change

  • puts the image you want to convert in place of img, in imagem is the name of the variable you will store, then you will send to the bank the variable imagem

  • So I left the code: Imageconverter converter = new Imageconverter(); image = (byte[])convert.Convertto(imagemPictureBox, typeof(byte[]); ...

Browser other questions tagged

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