Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a combobox in c# winform that retrieve information a datable, the code works fine, but after a while it start throwing system out of memory exception... I have attached my codes below

What I have tried:

try
{
    {
        SqlDataAdapter da1 = new SqlDataAdapter("Select * from staff.Staffs where departmentID = '" + DepartmentIDLabel.Text + "'  ", con);
        dt1 = new DataTable();
        da1.Fill(dt1);
        DataRow row = dt1.NewRow();
        row["FullName"] = "";
        dt1.Rows.InsertAt(row, 0);
        cbServingStaff.DataSource = dt1;
        cbServingStaff.DisplayMember = "FullName";
        cbServingStaff.ValueMember = "id";
        con.Close();
    }
}
catch (SqlException exp)
{
    MessageBox.Show("Error retrieving data, details: " + exp.Message);
}
catch (System.Exception exp)
{
    MessageBox.Show("Error occured. Details: " + exp.Message);
}
Posted
Updated 18-Jan-22 8:59am
v2
Comments
[no name] 18-Jan-22 13:41pm    
Connection is an IDisposable; I don't see you doing any (disposing).
Ese Ochuko 18-Jan-22 14:57pm    
Disposing how please?
[no name] 19-Jan-22 2:22am    
https://kencenerelli.wordpress.com/2011/04/15/using-statements-to-wrap-your-database-connections/
Maciej Los 18-Jan-22 14:57pm    
How can you be sure that above code is causing "Out Of Memory Exception" error?
Ese Ochuko 18-Jan-22 15:06pm    
Because I use a try and catch to get the it...

First things first, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that through your whole application before you go any further!

When you've done that, start think about what you are doing, and when you are doing it: how much data are you retrieving, and what event is being handled when you do.

If you are loading a lot of data into a combobox that might be your problem; as may doing it in a timer or similar event - so check how much you are fetching as each row may require a new window handle and they are scarce resources that will runout long before actual memory does, but will throw the same "out of memory" error.

Use the debugger, and see what you can find - we can't do that for you because we have no access to your data or your app while it is running and you need both to even start looking at why it is happening, much less how to fix it!
 
Share this answer
 
Comments
Ese Ochuko 18-Jan-22 15:02pm    
Thank you very much... I appreciate the details... But my data is not up to 20 records and I am fetching it in the Form load event.
OriginalGriff 18-Jan-22 15:07pm    
And what do you do in the SelectedIndexChanged event for the Combobox for example?
Because loading the combo will cause one and if that does something that affects the content (even if it loads the same data) then you could start running out.

Seriously, you need to use the debugger to look at this, and we can't do that for you!
Please, read my comment first. There might be several reasons of above error.

I'd suggest to read this: Debugging System.OutOfMemoryException using .NET tools[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900