Button to insert records into the Database

Asked

Viewed 46 times

0

Great, I’m running a project for my PEF that consists of creating a BD 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 Datatimepicker parameter to Datetime What can I do? I leave you the following image that contains and the image of 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;
                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;

Thanks for the help. Hug

inserir a descrição da imagem aqui

1 answer

0


Change the line:

command.Parameters.Add("@DataDeNascimento", SqlDbType.DateTime).Value = dataDeNascimentoDateTimePicker; 

To:

command.Parameters.Add("@DataDeNascimento", SqlDbType.DateTime).Value = dataDeNascimentoDateTimePicker.Value; 
  • Good, I did what you suggested and another error appeared to me... this time as: Failed to convert the value of the Bitmap parameter to Byte[]. I guess it’s the Image not?

  • Yes, you need to check the return type of "imagePictureBox.Image" and convert it to the appropriate columnar value of your BD.

  • Forgive my ignorance, but I’m still a beginner in programming... How do I accomplish what you explained?

Browser other questions tagged

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