I am trying to upload an image to a mysql database. I upload the image on a asp.net page . But i am getting this error whenever i try to upload the image to the database.
System.FormatException: 'Input string was not in a correct format.'
on the line with this code
MySqlDataReader MyReader2 = MyCommand2.ExecuteReader();
This is the complete code that i tried
protected void upload_files(object sender, EventArgs e)
{
if (FileUpload_UploadFile.HasFile)
{
HttpPostedFile img = FileUpload_UploadFile.PostedFile;
// IMPLEMENT YOUR CODE FOR SAVING THE FILE
Console.Write(img);
Stream stream = img.InputStream;
BinaryReader binaryReader = new BinaryReader(stream);
bytes = binaryReader.ReadBytes((int)stream.Length);
result = System.Text.Encoding.UTF8.GetString(bytes);
myLabel2.Text = "file has been uploaded";
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string sql_command = "Insert INTO Recognized_person1 (Name,Image,Department,Department_id) VALUES (@Name, @Image, @Department, @Department_id )";
conn.Open();
MySqlCommand MyCommand2 = new MySqlCommand(sql_command, conn);
MyCommand2.Parameters.AddWithValue("@Name", MySqlDbType.VarChar).Value = Name.Text;
MyCommand2.Parameters.AddWithValue("@Image", MySqlDbType.Blob).Value = bytes;
MyCommand2.Parameters.AddWithValue("@Department", MySqlDbType.VarChar).Value = Department.Text ;
MyCommand2.Parameters.AddWithValue("@Department_id", MySqlDbType.Int32).Value = Int32.Parse((Department_id1.Text));
//MyCommand2.ExecuteNonQuery();
MySqlDataReader MyReader2 = MyCommand2.ExecuteReader();
while (MyReader2.Read())
{
}
conn.Close();
}
}
Any ideas on how to correct this error ?