System.OutofMemoryException

G

Guest

This is for a Win form.

Before I made changes to my text boxes this code worked. I don't think I
touch any code relating to my datagrid. Code should populate my datagrid
with blank data, this use to work but now I get a
"System.OutofMemoryException", on the DataRow dr = dt.NewRow();

DataTable dt = dgPrivileges.DataSource as DataTable;
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);
dt.Clear();


I also tried deleting the datagrid (dgPrivileges) and created a new datagrid
and named it dgPrivileges, but that didn't change anything.
 
K

Kevin Yu [MSFT]

Hi Cadel,

It seems that you're using dt.Clear at the end of the code. dt.Clear will
clear all data values in the DataTable which is acting as the datasource of
the DataGrid. That's why the DataGrid is showing nothing. Please remove
this line and try again.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

Sorry, Cadel, I didn't read your post clearly. There seems to be some
misunderstanding to me. So, the desired behavior is to get an empty
DataGrid. Have you tried to call the dt.Clear directly without adding an
extra row? Or is there any other controls that bind to the DataTable? For
example, your user control text box?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

I added a try catch to get a better error message.

Here is my new code, it errors out on DataRow dr = dt.NewRow(), with the
error, "Object reference not set to an instance of an object."


DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

dt.Clear();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}




I commented out adding a row to the datagrid, but the same error came up on
dt.Clear();




DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
// DataRow dr = dt.NewRow();
//
// dr[0] = 0;
// dr[1] = 0;
// dr[2] = 0;
// dr[3] = 0;
// dr[4] = 0;
// dr[5] = 0;
// dr[6] = 0;
//
// dt.Rows.Add(dr);

dt.Clear();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
 
K

Kevin Yu [MSFT]

Hi Cadel,

The NullReferenceException means dt is referring to null. This is because
the dgPrivileges.DataSource was not sucessfully casted to a DataTable type,
so dgPrivileges.DataSource as DataTable returns a null. Please try to check
if dgPrivileges.DataSource is a DataTable type.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Thanks for directing me to the problem. The code to create the datagrid
tabletype was not being executed.

DataTable dtGrid = ds.Tables[0].Clone();
dgPrivileges.DataSource = dtGrid;

Thanks for your help.


Thanks for you help.
 
K

Kevin Yu [MSFT]

You're welcome.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top