I have a problem with the code c# below, i should save a binary image in a sql server database 2014, i did the function to convert the image to binary, the image select it with a button, The problem is that when I save the database to 0x00 in the field Immagine, how can I fix this type of error? the Immagine field format is binary
QUERY:
private void buttonCaricaImmagine_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
//For any other formats
of.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG";
if (of.ShowDialog() == DialogResult.OK)
{
pictureBoxRapportino.ImageLocation = of.FileName;
imm = pictureBoxRapportino.Image;
checkImage = 1;
}
}
public byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
ImageConverter _imageConverter = new ImageConverter();
byte[] xByte = (byte[])_imageConverter.ConvertTo(imageIn, typeof(byte[]));
return xByte;
}
private void buttonInserimento_Click(object sender, EventArgs e)
{
try
{
if(checkImage==1 && textBoxNumeroDocumento.Text != "")
{
//controlla dati
int NumeroDocumento = int.Parse(textBoxNumeroDocumento.Text);
byte[] ImmagineBinaria = ImageToByteArray(imm);
string BinaryImageCast = Encoding.UTF8.GetString(ImmagineBinaria);
//MessageBox.Show("Immagine in formato binario: " + BinaryImageCast);
//inserisco i dati nel database
SqlConnection conn = db.apriconnessione();
String query = "Insert into Rapporto(IdCantiere,IdUtenteCreazione,NumeroDocumento,Data,Immagine) values(@IdCantiere,@IdUtenteCreazione,@NumeroDocumento,@Data,@Immagine) ";
using (SqlCommand command = new SqlCommand(query, conn))
{
command.Parameters.Add("@IdCantiere", SqlDbType.Int).Value = IdCantiere;
command.Parameters.Add("@IdUtenteCreazione", SqlDbType.Int).Value = u.IdUtente;
command.Parameters.Add("@NumeroDocumento", SqlDbType.Int).Value = int.Parse(textBoxNumeroDocumento.Text);
command.Parameters.Add("@Data", SqlDbType.DateTime).Value = dateTimePickerData.Value;
command.Parameters.Add("@Immagine", SqlDbType.Binary).Value = ImmagineBinaria;
command.ExecuteNonQuery();
}
db.chiudiconnessione();
conn.Close();
}
else
{
MessageBox.Show("Devi compilare tutti i campi");
}
}
catch(Exception ex)
{
MessageBox.Show("Errore: controlla i formati "+ex);
}
}
Table Schema
CREATE TABLE Rapporto(
IdRapporto int IDENTITY(1,1) PRIMARY KEY,
IdCantiere int FOREIGN KEY REFERENCES Cantiere(IdCantiere),
IdUtenteCreazione int FOREIGN KEY REFERENCES Utente(IdUtente),
NumeroDocumento varchar(5000) default NULL,
Data datetime default NULL,
Immagine varbinary(MAX) default NULL,
);

binary? it would have to be thevarbinary(max)or (obsolete) animagedata type.