473,289 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Sync between DataGrid and DB

Hi everybody,
I need help for this problem:
I'm using a DataGrid to Insert/Modify/Cancel data from a single table.
At the moment, every cell modification of a pre-existing row is correctly
update on the DB.
I have some doubts about inserting a new row.
The table has a Id field that is a primary key defined ad Identity
properties on SQLServer engine.
This column isn't visible on the grid and a properties DefaultValue=0 is
defined.
How can I synchronise the DataTable and the DB.
If I understood right I must sync after a row inserting on DataTable
otherwise I can insert another row on DataTable (duplicate Key on Id Column,
obviously).
I tried to catch a RowChanged event forcing a Adapter.Update() but I didn't
meet my goal.
Any suggestion ??
Thanks in advance, Pierluigi.
Nov 16 '05 #1
7 2657
Pierluigi,

In this case, you might have to refresh the whole table, just so that
you can get your id's right. Actually, you would have to. The only way to
prevent this from happening is to handle the updates yourself, instead of
through a data adapter, so that you can actually try and get the last id
inserted (in SQL Server, you can select @@identity to get the last identity
column inserted).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Pierluigi Terzoli" <pi***************@hotmail.com> wrote in message
news:O5****************@TK2MSFTNGP09.phx.gbl...
Hi everybody,
I need help for this problem:
I'm using a DataGrid to Insert/Modify/Cancel data from a single table.
At the moment, every cell modification of a pre-existing row is correctly
update on the DB.
I have some doubts about inserting a new row.
The table has a Id field that is a primary key defined ad Identity
properties on SQLServer engine.
This column isn't visible on the grid and a properties DefaultValue=0 is
defined.
How can I synchronise the DataTable and the DB.
If I understood right I must sync after a row inserting on DataTable
otherwise I can insert another row on DataTable (duplicate Key on Id
Column,
obviously).
I tried to catch a RowChanged event forcing a Adapter.Update() but I
didn't
meet my goal.
Any suggestion ??
Thanks in advance, Pierluigi.

Nov 16 '05 #2
Thanks Nicholas to reply me so soon.
Maybe I'm getting wrong, but even working through a Data Adapter should in
the way you explained.
If you left VisualStudio.NET create the DataAdapter you find a Insert
Statement who:
- INSERT a new record
- SELECT ... where Id=@@IDENTITY to refresh with the new data
This happen everytime I want to insert a record by DataAdapter.
Obviously the Insert Statement described above is invoked only by
Adapter.Update() method.
The method check the differences between the DataTable and the DB and call
the necessary Insert/Update/Delete SQL Statement.
I don't have this behavior and I' dont' understand why ?? Grrrr !! ;))
Pierluigi.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> ha
scritto nel messaggio news:OA*************@TK2MSFTNGP12.phx.gbl...
Pierluigi,

In this case, you might have to refresh the whole table, just so that
you can get your id's right. Actually, you would have to. The only way to prevent this from happening is to handle the updates yourself, instead of
through a data adapter, so that you can actually try and get the last id
inserted (in SQL Server, you can select @@identity to get the last identity column inserted).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Pierluigi Terzoli" <pi***************@hotmail.com> wrote in message
news:O5****************@TK2MSFTNGP09.phx.gbl...
Hi everybody,
I need help for this problem:
I'm using a DataGrid to Insert/Modify/Cancel data from a single table.
At the moment, every cell modification of a pre-existing row is correctly update on the DB.
I have some doubts about inserting a new row.
The table has a Id field that is a primary key defined ad Identity
properties on SQLServer engine.
This column isn't visible on the grid and a properties DefaultValue=0 is
defined.
How can I synchronise the DataTable and the DB.
If I understood right I must sync after a row inserting on DataTable
otherwise I can insert another row on DataTable (duplicate Key on Id
Column,
obviously).
I tried to catch a RowChanged event forcing a Adapter.Update() but I
didn't
meet my goal.
Any suggestion ??
Thanks in advance, Pierluigi.


Nov 16 '05 #3
Are you using a typed dataset?

"This column isn't visible on the grid and a properties DefaultValue=0 is
defined."

Use the generate dataset link of the dataadapter to generate the typed
dataset.

Normally a generated typed dataset in a indentity column place the following
parameter:

msdata:AutoIncrement="true"
Visualstudio dataadapter wizard add a select after the insert/update
statment to retrive the new id, the row id in the dataset will be updated
automatically

"Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im Newsbeitrag
news:O5****************@TK2MSFTNGP09.phx.gbl...
Hi everybody,
I need help for this problem:
I'm using a DataGrid to Insert/Modify/Cancel data from a single table.
At the moment, every cell modification of a pre-existing row is correctly
update on the DB.
I have some doubts about inserting a new row.
The table has a Id field that is a primary key defined ad Identity
properties on SQLServer engine.
This column isn't visible on the grid and a properties DefaultValue=0 is
defined.
How can I synchronise the DataTable and the DB.
If I understood right I must sync after a row inserting on DataTable
otherwise I can insert another row on DataTable (duplicate Key on Id Column, obviously).
I tried to catch a RowChanged event forcing a Adapter.Update() but I didn't meet my goal.
Any suggestion ??
Thanks in advance, Pierluigi.

Nov 16 '05 #4

Hi Zurcher,
I'm not using a typed dataset, I'm just using a DataTable I declared on my
source.
But I don't think this is the mistake.
You're right I already have my DataAdapter with Insert/Update Statement that
retrive the new id.
I need to see just a piece of source explaining how to insert a row in a
DataGrid.
What a Have to do, what choice ?
1. leave the user insert directly on the DataGrid and invoke
DataAdapter.Update();
2. Provides a Insert Button and Insert by DataTable.NewRow() and refresh the
DataGrid.
3. Provides a Insert Button and invoke a StoredProcedure who insert the new
record and rebind the DataSource
I don't understand how yet !
Apologise my understanding.
Pierluigi.

"Zürcher See" <aq****@cannabismail.com> ha scritto nel messaggio
news:%2****************@TK2MSFTNGP15.phx.gbl...
Are you using a typed dataset?

"This column isn't visible on the grid and a properties DefaultValue=0 is
defined."

Use the generate dataset link of the dataadapter to generate the typed
dataset.

Normally a generated typed dataset in a indentity column place the following parameter:

msdata:AutoIncrement="true"
Visualstudio dataadapter wizard add a select after the insert/update
statment to retrive the new id, the row id in the dataset will be updated
automatically

"Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im Newsbeitrag
news:O5****************@TK2MSFTNGP09.phx.gbl...
Hi everybody,
I need help for this problem:
I'm using a DataGrid to Insert/Modify/Cancel data from a single table.
At the moment, every cell modification of a pre-existing row is correctly update on the DB.
I have some doubts about inserting a new row.
The table has a Id field that is a primary key defined ad Identity
properties on SQLServer engine.
This column isn't visible on the grid and a properties DefaultValue=0 is
defined.
How can I synchronise the DataTable and the DB.
If I understood right I must sync after a row inserting on DataTable
otherwise I can insert another row on DataTable (duplicate Key on Id

Column,
obviously).
I tried to catch a RowChanged event forcing a Adapter.Update() but I

didn't
meet my goal.
Any suggestion ??
Thanks in advance, Pierluigi.


Nov 16 '05 #5
"Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im Newsbeitrag
news:OT**************@tk2msftngp13.phx.gbl...

Hi Zurcher,
I'm not using a typed dataset, I'm just using a DataTable I declared on my
source.
But I don't think this is the mistake.
You're right I already have my DataAdapter with Insert/Update Statement that retrive the new id.
I need to see just a piece of source explaining how to insert a row in a
DataGrid.
What a Have to do, what choice ?
1. leave the user insert directly on the DataGrid and invoke
DataAdapter.Update();
To my opinion this is the best solution.
Add an "Update" button, when clicked call the DataAdpter on the DataTable
2. Provides a Insert Button and Insert by DataTable.NewRow() and refresh the DataGrid.
Only if you have the impression that with a button all is more clearly
3. Provides a Insert Button and invoke a StoredProcedure who insert the new record and rebind the DataSource
I don't like stored procedure.
If you want to add some default value, you can always add them to the new
DataRow, before add it to the DataTable

DataRow row=DataTable.NewRow();

//values that will be stored in db and never changed
row["IdOfUserWhoCreatedThisRow"]=userId;
row["DateOfCreation"]=DateTime.Now

DataTable.Add(row);

About DataTable event:

The DataTable are like double buffered, when you change a DataTable you can
alway reject all the changes.
The events take place when you acepts the chages to the DataTable.

(Do you speak italian?)
I don't understand how yet !
Apologise my understanding.
Pierluigi.

"Zürcher See" <aq****@cannabismail.com> ha scritto nel messaggio
news:%2****************@TK2MSFTNGP15.phx.gbl...
Are you using a typed dataset?

"This column isn't visible on the grid and a properties DefaultValue=0 is
defined."

Use the generate dataset link of the dataadapter to generate the typed
dataset.

Normally a generated typed dataset in a indentity column place the

following
parameter:

msdata:AutoIncrement="true"
Visualstudio dataadapter wizard add a select after the insert/update
statment to retrive the new id, the row id in the dataset will be updated automatically

"Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im Newsbeitrag news:O5****************@TK2MSFTNGP09.phx.gbl...
Hi everybody,
I need help for this problem:
I'm using a DataGrid to Insert/Modify/Cancel data from a single table.
At the moment, every cell modification of a pre-existing row is

correctly update on the DB.
I have some doubts about inserting a new row.
The table has a Id field that is a primary key defined ad Identity
properties on SQLServer engine.
This column isn't visible on the grid and a properties DefaultValue=0 is defined.
How can I synchronise the DataTable and the DB.
If I understood right I must sync after a row inserting on DataTable
otherwise I can insert another row on DataTable (duplicate Key on Id

Column,
obviously).
I tried to catch a RowChanged event forcing a Adapter.Update() but I

didn't
meet my goal.
Any suggestion ??
Thanks in advance, Pierluigi.



Nov 16 '05 #6

Thanks again Zurcher,
so, a few minutes I received the last your post a solved (for the moment)
using the second of my options:
- I provide a button
- On click
* Create a DataRow
* Column Initialization (to respect DB constrain)
* DataTable.InsertAt(..)
* DataAdapter.Update(..)
Yes, I'm Italian ;)
Did you realise it by reading my bad english ?? ;)
Pierluigi.

"Zürcher See" <aq****@cannabismail.com> ha scritto nel messaggio
news:Oe**************@TK2MSFTNGP15.phx.gbl...
"Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im Newsbeitrag
news:OT**************@tk2msftngp13.phx.gbl...

Hi Zurcher,
I'm not using a typed dataset, I'm just using a DataTable I declared on my
source.
But I don't think this is the mistake.
You're right I already have my DataAdapter with Insert/Update Statement that
retrive the new id.
I need to see just a piece of source explaining how to insert a row in a
DataGrid.
What a Have to do, what choice ?
1. leave the user insert directly on the DataGrid and invoke
DataAdapter.Update();


To my opinion this is the best solution.
Add an "Update" button, when clicked call the DataAdpter on the DataTable
2. Provides a Insert Button and Insert by DataTable.NewRow() and refresh

the
DataGrid.


Only if you have the impression that with a button all is more clearly
3. Provides a Insert Button and invoke a StoredProcedure who insert the

new
record and rebind the DataSource


I don't like stored procedure.
If you want to add some default value, you can always add them to the new
DataRow, before add it to the DataTable

DataRow row=DataTable.NewRow();

//values that will be stored in db and never changed
row["IdOfUserWhoCreatedThisRow"]=userId;
row["DateOfCreation"]=DateTime.Now

DataTable.Add(row);

About DataTable event:

The DataTable are like double buffered, when you change a DataTable you

can alway reject all the changes.
The events take place when you acepts the chages to the DataTable.

(Do you speak italian?)
I don't understand how yet !
Apologise my understanding.
Pierluigi.

"Zürcher See" <aq****@cannabismail.com> ha scritto nel messaggio
news:%2****************@TK2MSFTNGP15.phx.gbl...
Are you using a typed dataset?

"This column isn't visible on the grid and a properties DefaultValue=0
is defined."

Use the generate dataset link of the dataadapter to generate the typed
dataset.

Normally a generated typed dataset in a indentity column place the

following
parameter:

msdata:AutoIncrement="true"
Visualstudio dataadapter wizard add a select after the insert/update
statment to retrive the new id, the row id in the dataset will be updated automatically

"Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im Newsbeitrag news:O5****************@TK2MSFTNGP09.phx.gbl...
> Hi everybody,
> I need help for this problem:
> I'm using a DataGrid to Insert/Modify/Cancel data from a single table. > At the moment, every cell modification of a pre-existing row is

correctly
> update on the DB.
> I have some doubts about inserting a new row.
> The table has a Id field that is a primary key defined ad Identity
> properties on SQLServer engine.
> This column isn't visible on the grid and a properties
DefaultValue=0 is > defined.
> How can I synchronise the DataTable and the DB.
> If I understood right I must sync after a row inserting on DataTable
> otherwise I can insert another row on DataTable (duplicate Key on Id
Column,
> obviously).
> I tried to catch a RowChanged event forcing a Adapter.Update() but I
didn't
> meet my goal.
> Any suggestion ??
> Thanks in advance, Pierluigi.
>
>



Nov 16 '05 #7
Dal nome visto che sono italiano anch'io, il mio nome non lo scrivo, non mi
piace lo spam, "Zurcher See" non vuol dire altro che "Lago di Zuigo" in
tedesco

"Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im Newsbeitrag
news:eK**************@tk2msftngp13.phx.gbl...

Thanks again Zurcher,
so, a few minutes I received the last your post a solved (for the moment)
using the second of my options:
- I provide a button
- On click
* Create a DataRow
* Column Initialization (to respect DB constrain)
* DataTable.InsertAt(..)
* DataAdapter.Update(..)
Yes, I'm Italian ;)
Did you realise it by reading my bad english ?? ;)
Pierluigi.

"Zürcher See" <aq****@cannabismail.com> ha scritto nel messaggio
news:Oe**************@TK2MSFTNGP15.phx.gbl...
"Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im Newsbeitrag
news:OT**************@tk2msftngp13.phx.gbl...

Hi Zurcher,
I'm not using a typed dataset, I'm just using a DataTable I declared on
my source.
But I don't think this is the mistake.
You're right I already have my DataAdapter with Insert/Update
Statement
that
retrive the new id.
I need to see just a piece of source explaining how to insert a row in
a DataGrid.
What a Have to do, what choice ?
1. leave the user insert directly on the DataGrid and invoke
DataAdapter.Update();


To my opinion this is the best solution.
Add an "Update" button, when clicked call the DataAdpter on the

DataTable
2. Provides a Insert Button and Insert by DataTable.NewRow() and refresh
the
DataGrid.


Only if you have the impression that with a button all is more clearly
3. Provides a Insert Button and invoke a StoredProcedure who insert
the new
record and rebind the DataSource


I don't like stored procedure.
If you want to add some default value, you can always add them to the new DataRow, before add it to the DataTable

DataRow row=DataTable.NewRow();

//values that will be stored in db and never changed
row["IdOfUserWhoCreatedThisRow"]=userId;
row["DateOfCreation"]=DateTime.Now

DataTable.Add(row);

About DataTable event:

The DataTable are like double buffered, when you change a DataTable you

can
alway reject all the changes.
The events take place when you acepts the chages to the DataTable.

(Do you speak italian?)
I don't understand how yet !
Apologise my understanding.
Pierluigi.

"Zürcher See" <aq****@cannabismail.com> ha scritto nel messaggio
news:%2****************@TK2MSFTNGP15.phx.gbl...
> Are you using a typed dataset?
>
> "This column isn't visible on the grid and a properties
DefaultValue=0 is
> defined."
>
> Use the generate dataset link of the dataadapter to generate the

typed > dataset.
>
> Normally a generated typed dataset in a indentity column place the
following
> parameter:
>
> msdata:AutoIncrement="true"
>
>
> Visualstudio dataadapter wizard add a select after the insert/update
> statment to retrive the new id, the row id in the dataset will be

updated
> automatically
>
> "Pierluigi Terzoli" <pi***************@hotmail.com> schrieb im

Newsbeitrag
> news:O5****************@TK2MSFTNGP09.phx.gbl...
> > Hi everybody,
> > I need help for this problem:
> > I'm using a DataGrid to Insert/Modify/Cancel data from a single

table. > > At the moment, every cell modification of a pre-existing row is
correctly
> > update on the DB.
> > I have some doubts about inserting a new row.
> > The table has a Id field that is a primary key defined ad Identity
> > properties on SQLServer engine.
> > This column isn't visible on the grid and a properties

DefaultValue=0
is
> > defined.
> > How can I synchronise the DataTable and the DB.
> > If I understood right I must sync after a row inserting on DataTable > > otherwise I can insert another row on DataTable (duplicate Key on Id > Column,
> > obviously).
> > I tried to catch a RowChanged event forcing a Adapter.Update() but I > didn't
> > meet my goal.
> > Any suggestion ??
> > Thanks in advance, Pierluigi.
> >
> >
>
>



Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kartik | last post by:
I open an fstream in read-only mode, read till the end, then try to sync() before seeking to position 0 & reading again. But the sync fails. I discovered that clear()ing the stream before the sync...
0
by: berullus | last post by:
I created a datagrid in windows form and the datasource for it is a datatable. It works fine when I add a new row in the grid and click on Save button which I added on the form to save as xml file....
3
by: AH | last post by:
Hi, I post this again to hope for valuable help. I have a window form order entry type of application where I used a datagrid to capture the order line detail. the datasource of this datagrid is...
2
by: Gene Hubert | last post by:
I'm assigning a new datasource (a datatable) to a datagrid that is aready displayed in a form. Then I assign a new value to CurrentRowIndex. The datagrid always scrolls so that the new current...
2
by: rodchar | last post by:
hey all, if i have a datagrid in one browser, click on a record, and it opens another browser to view the details, update the details in that browser and close it, how do i auto update the...
3
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also...
7
by: Ole | last post by:
Hi, I'm going to develop a socket communication between an instrument (running CE 5.0 with Compact Fraework V2) and a PC. As the instrument should only connect to one PC at a time I believe that...
3
by: iKiLL | last post by:
Hi , I am trying to debug a problem with a Windows Mobile Application. I need the PDA to Sync(I have writen the sync process myself using a web service) using GPRS while Debugging from the VS...
7
by: john | last post by:
I am reading TC++PL3 and on page 644 it is mentioned: "Flushing an istream is done using sync(). This cannot always be done right. For some kinds of streams, we would have to reread characters...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.