Memorystream parameter not valid in c#?
Hi guys,
I'm developing a system for our Human Resource Management. The system includes displaying of employee's profile with picture. But I got some trouble on the employee's picture. I have successfully stored it on mysql database, however, when I tried to retrieve it, an error occurred. MemoryStream parameter was not valid. Can anyone help me on this?Here is my code below:
string query = "select pic, picsize from employee";
MySqlConnection con = new MySqlConnection(db.getConnection());
MySqlCommand cmd = new MySqlCommand(query, con);
try
{
con.Open();
data = cmd.ExecuteReader();
if (data.Read())
{
int filesize = data.GetInt32(data.GetOrdinal("picsize"));
byte[] img = new byte[filesize];
data.GetBytes(data.GetOrdinal("pic"), 0, img, 0, filesize);
MemoryStream ms = new MemoryStream(img); //error goes here
pic.Image = Image.FromStream(ms);
}
con.Close();
}
catch
{
MessageBox.Show("Unable to load ID picture!", "Picture Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
finally
{
con.Close();
}