1

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 ?

Dibba Butt
  • 21
  • 2
  • Firstly, use `ExecuteNonQuery` as it is used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected. Secondly, The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer at `Department_id1.Text`. – Rahul Sharma Jun 19 '19 at 09:25
  • @RahulSharma , i'm getting the same error even when i remove department_id (int) and try and add just Name and department (both string) – Dibba Butt Jun 19 '19 at 09:49
  • Okay. Couple of things: 1:For File/Image to `Byte Array` you can use `File.ReadAllBytes()` instead of `InputStream`. See this question: https://stackoverflow.com/questions/2030847/best-way-to-read-a-large-file-into-a-byte-array-in-c/2030865#2030865 and 2: Pass `Byte Array` instead of Byte Array's `Length` in your `Image` parameter. – Rahul Sharma Jun 19 '19 at 09:59
  • Did you get this resolved? – Rahul Sharma Jun 20 '19 at 08:44
  • yes @RahulSharma , problem wasn't in reading the image as a byte array just changed " MyCommand2.Parameters.AddWithValue("@Image", MySqlDbType.Blob).Value = bytes;" to "MyCommand2.Parameters.AddWithValue("@Imagee", bytes);" and that fixed the issue – Dibba Butt Jun 21 '19 at 09:08

0 Answers0