"Invalid Parameter Used" error when reading images from MemoryStre

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I have to write images(.gif/.bmp/.jpg/.ico), to db and read them. Uploading
images to db works fine for me. Reading from db to byte[] is also ok. But,
when I try to display them in my form controls, I get this error.
This is how it goes :

<code>
byte[] bImage1 = (byte[])datasetImageList.Tables["IMAGE"].Rows[0]["IMAGE1"];
System.IO.MemoryStream ms = new MemoryStream(bImage1, 0, bImage1.Length);
this.lblRunImage.Image = System.Drawing.Image.FromStream(ms); // Exception
occurs here.
</code>

I have tested this with .gif and .bmp and it does not work in either case.
Please help me...
Thanks.
 
CNU,

Why are you so sure that the uploading is right,

On what way did you display them before?

(I ask this because I don't see direct strange things in your code for using
them).

Cor
 
Thanks Cor for the reply.

May be your point is valid. This is how I upload the file...

<code>
string Type = "MYIMAGE";
MemoryStream fs = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, @"c:\test.gif");
byte[] objImage1 = fs.ToArray();

// database actions
CDBHandler.m_sqlTransaction = this.m_sqlConnection.BeginTransaction();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = this.m_sqlConnection;
sqlCmd.Transaction = this.m_sqlTransaction;
sqlCmd.CommandText = "insert into Image (Type,Image1) values (@Type,@Image1)";

SqlParameter sqlParam1 = new SqlParameter();
sqlParam1 = new SqlParameter("@Type", SqlDbType.VarChar);
sqlParam1.Value = strType;
sqlCmd.Parameters.Add(sqlParam1);

SqlParameter sqlParam2 = new SqlParameter();
sqlParam2 = new SqlParameter("@Image1", SqlDbType.VarBinary);
sqlParam2.Value = objImage1;
sqlCmd.Parameters.Add(sqlParam2);

sqlCmd.ExecuteNonQuery();
this.m_sqlTransaction.Commit();
</code>

This is working fine. But, I noticed one thing while debugging...the size
of the byte[] while reading from the DB is more than the size of the byte[]
uploaded to the DB.
NOTE: I've edited the actual code since it is quite long and I cannot post
it as it is a part of our project. So, if something is messy, please let me
know.

Thanks.
 
cnu,

I have made a sample based on your code for you, that was easier than check
your code.

And now it is ready, I see that probably your second parameter format is
wrong. :-)
sqlParam2 = new SqlParameter("@Image1", SqlDbType.Image);
http://msdn.microsoft.com/library/d...f/html/frlrfsystemdatasqldbtypeclasstopic.asp

However see my sample everything is in it and it is more compact than what
is now in your program.

(It is direct a picture viewer however has as well all extras that you need
except the writing to the database).

\\\needs a picturebox on a form
private void Form1_Load(object sender, System.EventArgs e)
{
OpenFileDialog fo = new OpenFileDialog();
if (fo.ShowDialog() == DialogResult.OK)
{
System.IO.FileStream fs = new System.IO.FileStream
(fo.FileName, System.IO.FileMode.Open);
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] abyt = br.ReadBytes((int)fs.Length);
br.Close();
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("image",Type.GetType("System.Byte[]"));
dt.Rows.Add(dt.NewRow());
dt.Rows[0]["image"] = abyt;
Byte[] abyt2 = (byte[]) dt.Rows[0][0];
System.IO.MemoryStream ms2 = new System.IO.MemoryStream(abyt2);
pictureBox1.Image = Image.FromStream(ms2);
}
}
}

I hope this helps?

Cor
 
Thanks a lot Cor. I have tried your code and it just works perfect. But one
interesting thing is it does not work with .ico files.

Coming to my code, the reason why I have used SqlParameters is, I generate
the sql statements dynamically. I have used SqlDBType.VarBinary instead of
SqlDBType.Image because, the both support byte[] and max size supported by
VarBinary is more than Image.

Any way, I replaced my image uploading code with your code and
SqlDBType.VarBinary with SqlDBType.Image, but the error is occurring.
 
cnu,

It is normal that it does not work with ico files, I am not an imaging guy,
however that seems to be comletly different and is not an image that you can
show in the pic box. Although you can convert it to a byte array of course
and back, what you can do with any file.

However from your message I don't understand if any image does not work in
your program or only the ico.

Cor
 
It works well with .gif and .bmp. The only problem is with .ico.

Now, I have another problem on hand. If I try to insert like this, I am
getting Stac

<code>
string strSqlStatement = "SELECT * FROM IMAGE"
+ " where TYPE like 'EC%'"
+ " and UPDATEUSER='" + objImage.UpdateUser + "'";

sqlDataAdapter = new SqlDataAdapter(strSqlStatement, sqlConnection); // ***
System.StackOverflowException is raised here ***
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
DataSet ds = new DataSet("IMAGE");
sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
sqlDataAdapter.Fill(ds, "IMAGE");

DataRow dr = ds.Tables["IMAGE"].NewRow();
// objImage is a class object that stores all the values for the table
dr["Type"] = objImage.Type;
dr["Number"] = objImage.Number;
dr["Image1"] = objImage.Image1;
dr["Image2"] = objImage.Image2;
dr["Image3"] = objImage.Image3;
dr["UpdateUser"] = objImage.UpdateUser;
dr["UpdateTime"] = objImage.UpdateTime;

ds.Tables["IMAGE"].Rows.Add(dr);
int nRowCount = sqlDataAdapter.Update(ds, "IMAGE");
</code>
 
Cnu,

A stack overflow is mostly because an infinity loop.

However please do not add new questions to old, that makes it for people
using this newsgroup as a kind of reference impossible to use that.

Cor
 
Back
Top