المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Out of Memory exception..



C# Programming
05-06-2009, 05:30 PM
When I drop an item into a ListView I get an out of memory exception... but... I have them on the same form.. within the same TabControl (different tabs though).

Basically I am saving the file... if the item is to be sent to M&R then I set the MR column (BIT) to True, and if not, then False. This way I can tell which ListView the item should show up in... But I get an error on the drag and dropping to the second listview (the M&R one)


private void listViewFiles_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
newFile = new OpenFileDialog();
newFile.FileName = file;
SaveFileThread(file, newFile.SafeFileName, false);
newFile.Dispose();
}
bwLoadFiles.RunWorkerAsync();
}
private void lstMRFiles_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
newFile = new OpenFileDialog();
newFile.FileName = file;
SaveFileThread(file, newFile.SafeFileName, true);
newFile.Dispose();
}
bwLoadFiles.RunWorkerAsync();
}




Here is what it is calling:


private void SaveFileThread(string Filename, string SafeFile, bool MR)
{
RegistryAccess reg = new RegistryAccess();
SqlConnection conn = new SqlConnection(reg.ReturnConnection);
SqlCommand cmd = new SqlCommand("SELECT * FROM Inventory_Files WHERE FilesID=@FilesID AND [Filename]=@Filename AND MR=@MR", conn);

try
{
cmd.Parameters.AddWithValue("@FilesID", txtTAG.Text);
cmd.Parameters.AddWithValue("@Filename", SafeFile);
cmd.Parameters.AddWithValue("@MR", MR);

conn.Open();
SqlDataReader myReader = cmd.ExecuteReader();

if (myReader.HasRows)
{
if (MR) Invoke(new FilesLabelDelegate(FilesLabel), new object[] { "Did not save. File already exist.", true });
else Invoke(new FilesLabelDelegate(FilesLabel), new object[] { "Did not save. File already exist.", false });
}
else
{
conn.Close();
FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read);

decimal Size = fs.Length;
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
fs.Close();

if (MR) Invoke(new FilesLabelDelegate(FilesLabel), new object[] { "Saving file.. Size: " + Math.Round((Size / 1024) / 1024, 2) + "MB", true });
else Invoke(new FilesLabelDelegate(FilesLabel), new object[] { "Saving file.. Size: " + Math.Round((Size / 1024) / 1024, 2) + "MB", false });

conn = new SqlConnection(reg.ReturnConnection);
cmd = new SqlCommand("INSERT INTO Inventory_Files (FilesID, Saved_File, Filename, File_Size, MR) VALUES (@FilesID, @File, @FileName, @Size, @MR)", conn);
cmd.Parameters.AddWithValue("@FilesID", txtTAG.Text);
cmd.Parameters.AddWithValue("@File", buffer);
cmd.Parameters.AddWithValue("@FileName", SafeFile);
cmd.Parameters.AddWithValue("@Size", Size);
cmd.Parameters.AddWithValue("@MR", MR);
conn.Open();

int Success = cmd.ExecuteNonQuery();

if (Success > 0)
{
if (MR) Invoke(new FilesLabelDelegate(FilesLabel), new object[] { "Saved file.", true });
else Invoke(new FilesLabelDelegate(FilesLabel), new object[] { "Saved file.", false });
}
}
}
catch (Exception ex) { new Errors(ex).ShowDialog(); }
finally { conn.Close(); }
}