0

This is my code:

try
{ 
    con = new MySqlConnection(conname);
    con.Open();
    //
    string query = " Select monday_img_slot1 from faculty_attend_record where idfaculty='"+id+"'";
    MySqlCommand cmd = new MySqlCommand(query, con);

    MySqlDataReader da = cmd.ExecuteReader();

    if(da.HasRows)
    {
        da.Read();
        byte[] img = (byte[])da[0];
        // error is in the line below 
        MemoryStream ms = new MemoryStream(img);
        captureImage.Image = Image.FromStream(ms);
    }

    con.Close();
}
catch (Exception e)
{
    MessageBox.Show(e.ToString());
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
M.Hassam Yahya
  • 382
  • 2
  • 7
  • I tried your code it works fine for jpg,jpeg,png image ,might be your image format different you need to use image converter check this https://stackoverflow.com/questions/629955/parameter-not-valid-exception-loading-system-drawing-image – Mangesh Auti Jul 06 '19 at 13:24
  • Sir my Image datatype is longblob i tried every thing can you help me with this – M.Hassam Yahya Jul 06 '19 at 14:58
  • I also use same datatype longblob in database but what is image type which save in database ex. Jpg or .png etc – Mangesh Auti Jul 06 '19 at 15:41
  • image formate is jpg and the file size is 67B – M.Hassam Yahya Jul 06 '19 at 17:46
  • 67 bytes? That hardly sounds like a valid image. May I ask how big you think that image should be in terms of pixels? – Lasse V. Karlsen Jul 06 '19 at 20:47
  • Also, just for "laughs", could you post the actual bytes here? Do something like `Debug.WriteLine(BitConverter.ToString(img));` and then post the results? (don't do this if you think the image may be proprietary or something you don't want posted on the internet) – Lasse V. Karlsen Jul 06 '19 at 20:48
  • Yeah its size is 67 Bytes. Actually its working is my laptop camera capture the image and then store in mysql then i want to retrieve image from mysql but it is giving the mention error – M.Hassam Yahya Jul 06 '19 at 21:34
  • Debug.WriteLine(BitConverter.ToString(img)); 45-3A-44-61-74-61-50-72-6F-6A-65-63-74-43-23-6B-61-66-6F-6C-64-65-72-4D-6F-6E-64-61-79-53-6C-6F-74-20-31-20-28-38-3B-34-35-2D-31-30-3B-31-35-29-52-6F-6F-6D-5F-31-30-35-70-72-6F-63-65-73-73-2E-6A-70-67 It is showing this after i debug – M.Hassam Yahya Jul 06 '19 at 21:39
  • @M.HassamYahya can you check with different image .might be image you are using is corrupt one – Mangesh Auti Jul 07 '19 at 10:00
  • Ahhh... i think i get my mistake my image is not updating correctly can some on guidde me with updating img querry string captureimg = path; and query is string cmdname1 = "Update faculty_attend_record set monday_slot1='present',monday_img_slot1='"+captureimg+"', monday_class_slot1='"+classname1.Text+"', monday_room_slot1='"+RoomNo1.Text+"' where idfaculty='"+idfaculty+"'"; – M.Hassam Yahya Jul 07 '19 at 10:46

1 Answers1

0

You are storing image path in database (column monday_img_slot1) instead of actual image. Need to convert image into Byte first and then store it in database.

Below is one demo for how to upload Image in database ( table name="images" and "data" column is longblob ,"id" is int,"name" is string type)

Note:you need to use prepared statement to avoid mysql injection.

MySqlConnection con=null;
try
{
    string myConnectionString = "server=localhost;database=test;uid=root;pwd=root;";
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "Image files | *.jpg";
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {

        con = new MySqlConnection(myConnectionString);
        string FileName = openFileDialog1.FileName;
        FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        byte[] ImageData = br.ReadBytes((int)fs.Length);
        br.Close();
        fs.Close();
        string CmdString = "INSERT INTO images(id, name, data) VALUES(@id, @name, @data)";
        MySqlCommand cmd = new MySqlCommand(CmdString, con);
        cmd.Parameters.Add("@id", MySqlDbType.Int32);
        cmd.Parameters.Add("@name", MySqlDbType.VarChar, 45);
        cmd.Parameters.Add("@data", MySqlDbType.LongBlob);
        cmd.Parameters["@id"].Value = 5;
        cmd.Parameters["@name"].Value = textBox1.Text;
        cmd.Parameters["@data"].Value = ImageData;

        con.Open();
        int RowsAffected = cmd.ExecuteNonQuery();
        if (RowsAffected > 0)
        {
            MessageBox.Show("Image saved sucessfully!");
        }

    }
    else
    {
        MessageBox.Show("Incomplete data!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    if (con!=null && con.State == ConnectionState.Open)
    {
        con.Close();
    }
}

In your case before update command ,convert image into byte:

MySqlConnection con=null;
try
{
    con = new MySqlConnection(conname);

    string captureimg = path;
    FileStream fs = new FileStream(captureimg, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    byte[] ImageData = br.ReadBytes((int)fs.Length);
    br.Close();
    fs.Close();
    string CmdString = "Update faculty_attend_record set monday_slot1=@monday_slot1,monday_img_slot1=@monday_img_slot1,monday_class_slot1=@monday_class_slot1,monday_room_slot1=@monday_room_slot1 where idfaculty=@idfaculty";

    MySqlCommand cmd = new MySqlCommand(CmdString, con);
    cmd.Parameters.Add("@monday_slot1", MySqlDbType.VarChar, 50);
    cmd.Parameters.Add("@monday_img_slot1", MySqlDbType.LongBlob);
    cmd.Parameters.Add("@monday_class_slot1", MySqlDbType.VarChar, 50);
    cmd.Parameters.Add("@monday_room_slot1", MySqlDbType.VarChar, 50);
    cmd.Parameters.Add("@idfaculty", MySqlDbType.VarChar, 50);
    cmd.Parameters["@monday_slot1"].Value = "present";
    cmd.Parameters["@monday_img_slot1"].Value = ImageData;
    cmd.Parameters["@monday_class_slot1"].Value = classname1.Text;
    cmd.Parameters["@monday_room_slot1"].Value = RoomNo1.Text;
    cmd.Parameters["@idfaculty"].Value = idfaculty;
    con.Open();
    int RowsAffected = cmd.ExecuteNonQuery();
    if (RowsAffected > 0)
    {
        MessageBox.Show("Image saved sucessfully!");
    }
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    if (con!=null && con.State == ConnectionState.Open)
    {
        con.Close();
    }
}

Note: As I don't know fully datatype of columns present in your table, I assume it all string and one longbolb so you do modification according to your requirement.

Mangesh Auti
  • 1,123
  • 1
  • 7
  • 12