PC Review


Reply
Thread Tools Rate Thread

Concurrency violation (new try)

 
 
Vladimir Oľura
Guest
Posts: n/a
 
      28th Oct 2005
I am building a pocket pc application that requires a datagrid. I am
inserting a new row this way:


private void mInsert_Click(object sender, System.EventArgs e)
{
try
{
DataRow dr = this.ldb.DohvatiDataSet.Tables[tableName].NewRow();
dr.BeginEdit();
for(int i = 0; i <
this.ldb.DohvatiDataSet.Tables[tableName].Columns.Count; i++)
if(!this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].ReadOnly)
dr[i] =
this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].DefaultValue;
if(!this.cbImena.SelectedItem.ToString().Equals("<SVE OSOBE>"))
dr["Osoba"] = this.cbImena.SelectedItem.ToString();
if(!this.cbProjekt.SelectedItem.ToString().Equals("<SVE PROJEKTE>"))
dr["KratProjekt"] = this.cbProjekt.SelectedItem.ToString();
if(!this.cbVrstePosla.SelectedItem.ToString().Equals("<SVE VRSTE
POSLA>"))
dr["KratPosao"] = this.cbVrstePosla.SelectedItem.ToString();
if(this.checkBox1.Checked)
dr["DatPosao"] = this.dtp.Value.ToString();
dr.EndEdit();
this.ldb.DohvatiDataSet.Tables[tableName].Rows.Add(dr);
this.mSave_Click(this, System.EventArgs.Empty); // defined at the bottom
of this method
this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Sort = "";
this.dataGrid1.CurrentRowIndex =
this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Count - 1;
this.dataGrid1.ScrollToLeft();
}
catch(System.Data.DBConcurrencyException dbce)
{
MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

private void mSave_Click(object sender, System.EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.Deleted));

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.ModifiedCurrent));

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
)])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
DataViewRowState.Added));
this.ldb.DohvatiDataSet.AcceptChanges();
this.dataGrid1.Focus();
Cursor.Current = Cursors.Default;
this.UpdateAgreg();
}
catch(System.Data.DBConcurrencyException dbce)
{
if(Cursor.Current == Cursors.WaitCursor) Cursor.Current =
Cursors.Default;
MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

I only get a concurrency violation when I insert a new row and change data
in it, that save it. If no new rows are inserted I don't get a concurrency
violation. Please help!

Vladimir Oľura!


 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      28th Oct 2005

Hi,


I confess you that I did not fully read the code, but I bet that the
problem is that you are trying to insert a new row in the very same
collection you are iterating.
You have to store the new rows in a temp collection (like ArrayList) and
then outside the loop add them to the collection.

like:
ArrayList ar = new ArrayList()
foreach( DataRow row in Rows )
{
if ( addthis)
ar.Add( row)
}
foreach( DataRow row in ar)
Rows.Add( row);


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



"Vladimir Oľura" <(E-Mail Removed)> wrote in message
news:djthbj$g1g$(E-Mail Removed)...
>I am building a pocket pc application that requires a datagrid. I am
> inserting a new row this way:
>
>
> private void mInsert_Click(object sender, System.EventArgs e)
> {
> try
> {
> DataRow dr = this.ldb.DohvatiDataSet.Tables[tableName].NewRow();
> dr.BeginEdit();
> for(int i = 0; i <
> this.ldb.DohvatiDataSet.Tables[tableName].Columns.Count; i++)
> if(!this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].ReadOnly)
> dr[i] =
> this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].DefaultValue;
> if(!this.cbImena.SelectedItem.ToString().Equals("<SVE OSOBE>"))
> dr["Osoba"] = this.cbImena.SelectedItem.ToString();
> if(!this.cbProjekt.SelectedItem.ToString().Equals("<SVE PROJEKTE>"))
> dr["KratProjekt"] = this.cbProjekt.SelectedItem.ToString();
> if(!this.cbVrstePosla.SelectedItem.ToString().Equals("<SVE VRSTE
> POSLA>"))
> dr["KratPosao"] = this.cbVrstePosla.SelectedItem.ToString();
> if(this.checkBox1.Checked)
> dr["DatPosao"] = this.dtp.Value.ToString();
> dr.EndEdit();
> this.ldb.DohvatiDataSet.Tables[tableName].Rows.Add(dr);
> this.mSave_Click(this, System.EventArgs.Empty); // defined at the
> bottom
> of this method
> this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Sort = "";
> this.dataGrid1.CurrentRowIndex =
> this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Count - 1;
> this.dataGrid1.ScrollToLeft();
> }
> catch(System.Data.DBConcurrencyException dbce)
> {
> MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
> MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
> }
> }
>
> private void mSave_Click(object sender, System.EventArgs e)
> {
> try
> {
> Cursor.Current = Cursors.WaitCursor;
>
> ((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
> )])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
> DataViewRowState.Deleted));
>
> ((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
> )])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
> DataViewRowState.ModifiedCurrent));
>
> ((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
> )])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
> DataViewRowState.Added));
> this.ldb.DohvatiDataSet.AcceptChanges();
> this.dataGrid1.Focus();
> Cursor.Current = Cursors.Default;
> this.UpdateAgreg();
> }
> catch(System.Data.DBConcurrencyException dbce)
> {
> if(Cursor.Current == Cursors.WaitCursor) Cursor.Current =
> Cursors.Default;
> MessageBox.Show(dbce.Message.ToString(), "Error", MessageBoxButtons.OK,
> MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
> }
> }
>
> I only get a concurrency violation when I insert a new row and change data
> in it, that save it. If no new rows are inserted I don't get a concurrency
> violation. Please help!
>
> Vladimir Oľura!
>
>



 
Reply With Quote
 
Vladimir Oľura
Guest
Posts: n/a
 
      29th Oct 2005
I am not trying to insert a new row in the very same collection I am
iterating. Here are the steps I take to insert a new row:

1. Make a new row with the NewRow method.
2. Set the value of each column to the column default value for the new row
(this is where I iterate through the columns collection, not the rows
collection)
3. Add the new row with the Add method.
4. Call the Update method of the SqlCeDataAdapter
5. Call the AcceptChanges method.

Vladimir Oľura



"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:#njDpI#(E-Mail Removed)...
>
> Hi,
>
>
> I confess you that I did not fully read the code, but I bet that the
> problem is that you are trying to insert a new row in the very same
> collection you are iterating.
> You have to store the new rows in a temp collection (like ArrayList) and
> then outside the loop add them to the collection.
>
> like:
> ArrayList ar = new ArrayList()
> foreach( DataRow row in Rows )
> {
> if ( addthis)
> ar.Add( row)
> }
> foreach( DataRow row in ar)
> Rows.Add( row);
>
>
> cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
>
>
> "Vladimir Oľura" <(E-Mail Removed)> wrote in message
> news:djthbj$g1g$(E-Mail Removed)...
> >I am building a pocket pc application that requires a datagrid. I am
> > inserting a new row this way:
> >
> >
> > private void mInsert_Click(object sender, System.EventArgs e)
> > {
> > try
> > {
> > DataRow dr = this.ldb.DohvatiDataSet.Tables[tableName].NewRow();
> > dr.BeginEdit();
> > for(int i = 0; i <
> > this.ldb.DohvatiDataSet.Tables[tableName].Columns.Count; i++)
> > if(!this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].ReadOnly)
> > dr[i] =
> > this.ldb.DohvatiDataSet.Tables[tableName].Columns[i].DefaultValue;
> > if(!this.cbImena.SelectedItem.ToString().Equals("<SVE OSOBE>"))
> > dr["Osoba"] = this.cbImena.SelectedItem.ToString();
> > if(!this.cbProjekt.SelectedItem.ToString().Equals("<SVE PROJEKTE>"))
> > dr["KratProjekt"] = this.cbProjekt.SelectedItem.ToString();
> > if(!this.cbVrstePosla.SelectedItem.ToString().Equals("<SVE VRSTE
> > POSLA>"))
> > dr["KratPosao"] = this.cbVrstePosla.SelectedItem.ToString();
> > if(this.checkBox1.Checked)
> > dr["DatPosao"] = this.dtp.Value.ToString();
> > dr.EndEdit();
> > this.ldb.DohvatiDataSet.Tables[tableName].Rows.Add(dr);
> > this.mSave_Click(this, System.EventArgs.Empty); // defined at the
> > bottom
> > of this method
> > this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Sort = "";
> > this.dataGrid1.CurrentRowIndex =
> > this.ldb.DohvatiDataSet.Tables[tableName].DefaultView.Count - 1;
> > this.dataGrid1.ScrollToLeft();
> > }
> > catch(System.Data.DBConcurrencyException dbce)
> > {
> > MessageBox.Show(dbce.Message.ToString(), "Error",

MessageBoxButtons.OK,
> > MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
> > }
> > }
> >
> > private void mSave_Click(object sender, System.EventArgs e)
> > {
> > try
> > {
> > Cursor.Current = Cursors.WaitCursor;
> >
> >

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
> > )])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
> > DataViewRowState.Deleted));
> >
> >

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
> > )])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
> > DataViewRowState.ModifiedCurrent));
> >
> >

((SqlCeDataAdapter)(this.ldb.GetAdapterList[this.ldb.GetTableIndex(tableName
> > )])).Update(this.ldb.DohvatiDataSet.Tables[tableName].Select(null, null,
> > DataViewRowState.Added));
> > this.ldb.DohvatiDataSet.AcceptChanges();
> > this.dataGrid1.Focus();
> > Cursor.Current = Cursors.Default;
> > this.UpdateAgreg();
> > }
> > catch(System.Data.DBConcurrencyException dbce)
> > {
> > if(Cursor.Current == Cursors.WaitCursor) Cursor.Current =
> > Cursors.Default;
> > MessageBox.Show(dbce.Message.ToString(), "Error",

MessageBoxButtons.OK,
> > MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
> > }
> > }
> >
> > I only get a concurrency violation when I insert a new row and change

data
> > in it, that save it. If no new rows are inserted I don't get a

concurrency
> > violation. Please help!
> >
> > Vladimir Oľura!
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concurrency violation rj Microsoft VB .NET 0 22nd May 2006 08:03 AM
Concurrency Violation elziko Microsoft VB .NET 4 18th Feb 2005 12:47 PM
Concurrency violation Vik Microsoft ASP .NET 0 7th Dec 2004 08:53 PM
Re: CONCURRENCY VIOLATION One Handed Man Microsoft ADO .NET 1 18th Aug 2003 07:17 AM
Concurrency Violation Hill Cheung Microsoft ADO .NET 1 4th Jul 2003 09:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:26 AM.