views:

97

answers:

1

Hi, I'm using Visual C#2008 Express Edition and an Express SQL database. Every time I build my solution, I get an error like the one above. Obviously the file name changes. A new file is also created every time I hit a debug point.

I have a stored proc that gets every row from a database table, it gets these rows every time the main form initialises and Adds them to a Generics list. Without inserting or deleting from the table, it gets a different number of rows each time I start my windows application. The error started happening at the same time as the weird data retrieval issue. Any ideas at all about what can cause this?

Thanks

Jose,

Sure, here's my c# method, it retrieves every row in my table, each row has an int and and Image ....

    private List<ImageNumber> GetListOfKnownImagesAndNumbers()
    {
        //ImageNumber imNum = new ImageNumber();

        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = _conn;

        try
        {
            MemoryStream ms = new MemoryStream();

            sqlCommand.CommandText = "usp_GetKnownImagesAndValues";

            _conn.Open();
            using (IDataReader dr = sqlCommand.ExecuteReader())
            {
                while (dr.Read())
                {
                    ImageNumber imNum = new ImageNumber();
                    imNum.Value = dr.IsDBNull(dr.GetOrdinal("ImageValue")) ? 0 : Convert.ToInt32(dr["ImageValue"]);

                    //Turn the bitmap into a byte array
                    byte[] barrImg = (byte[])dr["ImageCaptured"];
                    string strfn = Convert.ToString(DateTime.Now.ToFileTime());
                    FileStream fs = new FileStream(strfn,
                                      FileMode.CreateNew, FileAccess.Write);
                    fs.Write(barrImg, 0, barrImg.Length);
                    fs.Flush();
                    fs.Close();
                    imNum.Image = (Bitmap)Image.FromFile(strfn);

                    _listOfNumbers.Add(imNum);
                }
                dr.Close();
                _conn.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            _conn.Close();
        }

        return _listOfNumbers;

    }

And here's my stored proc....

ALTER PROCEDURE dbo.usp_GetKnownImagesAndValues AS BEGIN

select  ImageCaptured, ImageValue
from CapturedImages

END

+1  A: 

Guys,

Thanks for looking at this. The answer in the end was to put a Thread.Sleep inside the while loop and it started working perfectly. There may be something else I could do, I am obviously waiting for something to complete which is why allowing more time has helped here. If I knew what needed to complete and how to detect when it had completed then I could check for that instead of simply waiting for a short time.