Strange error

  • Thread starter Thread starter John J. Hughes II
  • Start date Start date
J

John J. Hughes II

I have an application with MDI windows. The following code generates an
error on some tables on "ad.Fill(ds);" if the table has data and needs to be
cleared the first time. The second time the table is cleared there is no
error. This problem only occurs if the MDI window is maximized and does not
occur when the window is normal.

If I remove the check for data in the table and just clear the table the
error occurs the first time the data is loaded if the window is maximized.
If the window is normal size there is no error ever. If the windows is
maximized and the error occurs refreshing the data (calling the function a
second time) clears the error and works fine.

Since ad.Fill is an MS function I am clueless on where to look, any
suggestions? I can not understand what the size of the windows has to do
with anything.

Error message: (basically the data from the SQL is missing one record and
one record exists twice)
{"Failed to enable constraints. One or more rows contain values violating
non-null, unique, or foreign-key constraints." }

Code:
public static bool LoadData(Form frm, SqlConnection cn, SqlDataAdapter ad,
DataSet ds, string msg)
{
bool RetVal = false;

if(ds == null) return RetVal;

try
{
frm.Cursor = Cursors.WaitCursor;

Utils.MiscFuns.OpenConnection(cn, true, "RAMS");

bool clearFlag = false;
foreach(System.Data.DataTable dt in ds.Tables)
{
if(dt.Rows.Count > 0)
{
clearFlag = true;
break;
}
}

if(clearFlag)
ds.Clear();

ad.Fill(ds);

RetVal = true;
}
catch(Exception ex)
{ Utils.frmError.ShowError(msg, ex); }
finally
{
frm.Cursor = Cursors.Default;
cn.Close();
}

return RetVal;
}


Regards,
John
 
Hi John,

Is the dataset a strong typed dataset? Are there any constraints like
primary key or foreign key defined in the dataset? Additionally, before you
clear the dataset, if you call the dataset's AcceptChanges method, will
this help?

Normally, a windows's state (Maximized or normal) won't impact the a
dataset. Is there any controls which are data bound to the dataset? If
winodw's state is changed, is it possible that these controls's values are
also changed?

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Wow that worked, I'm amazed... Thanks!

Someday I will even figure out what adding AcceptChanges before a Clear has
to do with the screen being minimized or maximized. I can sort see where
accepting changes would cause the data set to be more stable but should this
not occur when the clear is called?

In regards to the other questions.:

1) Yes it's strongly typed.
2) Yes it has a constraint which is what generates the error. If you look
at the datagrid what seems to happen in the dataset is one record from the
SQL query is lost and another record is duplicated.
3) The dataset is bound to a datagrid. I would not think the datagrid would
know if the screen was maximized. It's docking is set to full for the area
it occupies and that does not change.

Regards,
John
 
Hi John,

We had better to call AcceptChanges before clear method, especially, the
dataset is bound to some control like text box and datagrid. This can
ensure the data in dataset are removed safely.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(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

Back
Top