|
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(); }
}
|
|
|
|
|
I would suspect this line
Jacob Dixon wrote: byte[] buffer = new byte[fs.Length];
You're creating a (contiguous) byte array big enough to hold the contents of a file. Worse, you're doing it in parallel for multiple files. I can see how this can quickly blow up memory consumption.
Just to isolate the problem, does running SaveFileThread sequentially help?
|
|
|
|
|
ok so if I create the
byte[] buffer;
outside the try/catch/finally.. then in the try I can create the
buffer = new byte[fs.length];
then in the finally I can set:
byte[] = null;
IT only does one at a time.. waiting for this part to get done before moving to the next file..
I did get it working though.. I didn't set the ListView to AllowDrop... and since I had to entire form to AllowDrop to put an image into a picturebox it was capturing it there. Seems to be working fine now... but what i changed will help with memory since I'm setting it to null at the end?
|
|
|
|
|
Jacob Dixon wrote: but what i changed will help with memory since I'm setting it to null at the end?
No, it shouldn't make any difference. Even if you don't set it to null, the CLR and the GC know enough to figure out that the byte array has no references outside your method, and array will therefore get garbage collected.
If just one file gives you an OutOfMemoryException, you should try streaming the contents of the file i.e. loading smaller chunks repeatedly, rather than loading it in one big byte array. I don't know enough ADO.NET to figure out how to stream it to the database though.
|
|
|
|
|
Hi
I am currently working on a project that requires me to read excel files from an external application. I am using the ExcelReader library (Excel Reader[^])
This works fine for spreadsheets <10mb (Others have reported problems only from around 300mb. This might be due to my spreadsheets being heavily loaded with macros, but that is just a hunch).
So I need one of two things:
1. A different library, one that can open (large) workbooks, identify sheets and get specific cell values (in the correct format). Thats all
OR:
2. I need the ExcelReader library to be able to handle large files. (it is otherwise really handy, so if this can be done it would be great)
Here is a code snippet of the method where it bombs out
internal static Int32[] ReadArrayOfInt32(BinaryReader reader, int count)
{
Int32[] data = new Int32[count];
for (int i = 0; i < data.Length; i++)
{
data[i] = reader.ReadInt32(); //throws EndOfStream Exception
}
return data;
}
Anyone have any suggestions?
Thanks
|
|
|
|
|
So you solved this issue?
Luis Fernando Forero G.
|
|
|
|
|
Hi Luis
If I remember correctly I ended up using another library called Taramon.Exceller which is basically a wrapper class for Excel interop. I initially tried to avoid this because it requires reading the entire excel file into memory whereas the ExcelReader class streams the required data directly (and much quickly).
Karl
|
|
|
|
|
OK I had a problem reading a large excel document.
The problem: The problem was who I was doing it.
Firs I loaded the excel file to the data base and for each of this excel I was making a validation.
The solution:
First I loaded the excel to a dt this data table had 4 columns as Keys and my validations whore to this DT so the 15000 row wore loaded an calculated in almost 30 secs
Thanks any way
Luis Fernando Forero G.
|
|
|
|
|
hi friends
I want to know how to convert C source code into C# source code
please..
|
|
|
|
|
Easy... 3 Steps...
1. Learn C Programming langauge
2. Learn C# Programming language
3. Re-Write the C Source into C# source
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
|
There are converters which can reduce the amount of work required, but you must have a good knowledge of both C/C++ and C# to get the job done.
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
|
|
|
|
|
hello, I need to call an Oracle FUNCTION which returns a REF CURSOR - tutorial?
closest I found is this, which illustrate stored proc (not function) with CURSOR output param (and use of OracleParameter with type=OracleType.Cursor)
http://support.microsoft.com/kb/322160[^]
Thanks
dev
|
|
|
|
|
|
hi friends,
i am working on c# .net and i need video cutter in my project.my project in c# .net and window application so if any one hacund code for that then please reply me.
thanks
|
|
|
|
|
I also need video cutter in my project. my project in c# .net and window application. so if any one can help me regarding this issue.
please reply me.
thanks.
|
|
|
|
|
Hi all,
TO my .net setup project i want to add a launch condition for crystal report installation.
In my prerequisities i selected Crystal reports fro basic visual studio 2008 (x64, x86)
in my debug folder i got those respective crystalreport10_5 folder after successful build.
i got CRRedist2008_x64.msi,CRRedist2008_x86.msi
i want to install CRRedist2008_x86.msi so in my Installurl i specified CRRedist2008_x86.msi's path.
but it is not installing, in installing window a message gathering information came and after some time it is closing.
so am trying to install CRRedist2005_x86.msi
but when am trying to install my setup project it always going to that crystal report launch condition though i installed CRRedist2005_x86.msi.
in my launch condition i gave properties as
Name : CrystalReport
Condition: CrystalReport
InstallUrl : CrystalReports10_5\CRRedist2005_x86.msi
Message: Do you want to install crystal Reports?
please provide launch condition to install crystal reports.
and why it is asking every time crystal report installation?
so that am not able to proceed further to install my application.
please help me out.
Thanks In Advance
|
|
|
|
|
Hdey pls let me know the answer if you will get the answer of your solution...
As i m also facing the same problem so..
pls reply earlyiest...
|
|
|
|
|
No dude, not yet
still am researching
no one providing necessary things.
|
|
|
|
|
Hello. I have a listview where I load data from sql database. ListView has two columns (name, surname) where data is loaded. On the selectedindexchanged event, when an item from the listview is selected it gets the other data from the database, based on selection, and writes it to several textboxes... basicly a phonebook. If I select someone from the list the info is displayed in the txtboxes, but if i click smeone a second time i get an exception "NullRefferenceException" "Object reference not set to an instance of an object."
private void lvNameList_SelectedIndexChanged(object sender, EventArgs e)
{
ClearSearch();
select = "SELECT * FROM agenda_telefonica WHERE nume = '" + lvNameList.FocusedItem.Text + "' AND prenume = '" + lvNameList.FocusedItem.SubItems[1].Text + "'";
SqlDataReader dr = realSql.Select(select);
if (!dr.HasRows)
{
MessageBox.Show("Information not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
while (dr.Read())
{
tbName.Text = dr[1].ToString().Trim() + " " + dr[2].ToString().Trim();
tbPhoneNumber1.Text = dr[3].ToString().Trim();
tbPhoneNumber2.Text = dr[4].ToString().Trim();
tbPhoneNumber3.Text = dr[5].ToString().Trim();
tbFaxNumber.Text = dr[6].ToString().Trim();
tbEmail.Text = dr[7].ToString().Trim();
tbCompany.Text = dr[8].ToString().Trim();
tbService.Text = dr[9].ToString().Trim();
tbEquipment.Text = dr[10].ToString().Trim();
}
}
dr.Close();
realSql.Close();
}
}
The exception is raised the second time i click an item in the list and points to the select string. Does it have something to do with the FocusedItem?
|
|
|
|
|
Before doing anything else check the if the SelectedItems.Count property is > 0.
|
|
|
|
|
Thanks.
|
|
|
|
|
You are closing realSql which is not opened in the method you have posted. It could therefore be realSql (null after closing) that is causing the exception.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
realSql is a dll refference to a sql class i've created. it has an select(string select) which open an sql connection then returns a datareader ("SqlDataReader dr = realSql.Select(select)"). So the realSql.Close() closes the connection opened by the Select() method.
|
|
|
|
|
i have to Make a GridView with print option (Inherit the GridView control and add a print method). The print button should print all the rows in the gridview. There should an option to print no of rows perpage.
. i have to use GDI printing APIs for printing.
Maifs
|
|
|
|