I have downloaded this PictureBox ImageArray
then I've added a button which will add all it's image to sql server 2005
this is the code:
private System.Windows.Forms.PictureBox[] imgArray2;
string c_string = "server=.\\sqlexpress;database=Blue;trusted_connection=true";
for (int i = 0; i < NumOfFiles; i++)
{
imgArray2[i].Image = Image.FromFile(imgName[i]);
string name = imgName[i].Substring(imgName[i].LastIndexOf(@"\") + 1, imgName[i].Length - imgName[i].LastIndexOf(@"\") - 1);
MemoryStream mstr = new MemoryStream();
imgArray2[i].Image.Save(mstr, imgArray2[i].Image.RawFormat);
byte[] arrImage = mstr.GetBuffer();
string cmd = "insert into Images (ImagePart" + i + "Name, ImagePart" + i + ") values (@PName, @Pic)";
SqlConnection c = new SqlConnection(c_string);
SqlCommand comm = new SqlCommand(cmd, c);
comm.Parameters.Add(new SqlParameter("@PName", SqlDbType.VarChar, 40)).Value = name;
comm.Parameters.Add(new SqlParameter("@Pic", SqlDbType.Image)).Value = arrImage;
try
{
c.Open();
comm.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException err)
{
MessageBox.Show(err.Message);
}
finally
{
c.Close();
}
}
I've added an imgArray2[] so the image from imgArray[] won't changes and even though I'm using imgArray[] it still throwing the Null error.
The error info highlights the imgArray2[i].Image = Image.FromFile(imgName[i]);
it shows nullreferenceexception was unhandled - Object reference not set to an instance of an object.