Weired Problems with Dataset and Dataviews

  • Thread starter Thread starter Lars Netzel
  • Start date Start date
L

Lars Netzel

hi!

myDataSet that is fillled from an Access 2000 db and includes ONE table

From that Table in myDataSet I create myDataView and use a Rowfilter to get
a few rows that i work with (i need a DataView cause I do a lot of
filtering)

The problem is the primary key field (a unique counter), let's call it
"TBL_ID".

If I, after using the Rowfilter, end up with a DataView that has Zero
records and I start adding a record (thru a datagrid) the TBL_ID field in
the DataView will always start on 0... and then count upwards .. 1, then 2,
then 3.. etc.

BUT... If I, after using the Rowfilter, end up with a DataView that has more
than one record, the TBL_ID field in the rows will be the right number from
the Table in the Access Database. If I get only one record and the TBL_ID is
54... the next new record I create will be 55 and etc.... This is how I want
it to act of course.

Why will myDataView start on 0 when it has no rows? It's created from the
Dataset that might have MANY records with ids...

How do I fix it? This is a big headache for me right now!

Best Regards
/Lars Netzel
 
Hi Lars,

Because the dataset is disconnected.
When you use autoincrement you would have to do always this

ds.update(xx)
ds.fill(xx) 'with this it gets the assigned numbers

After this it takes the last assigned number, even if another user has added
something between that in between.

Therefore the next added number on your grid is not sure the correct number
when it is not updated, because again another user can have added a row in
the table.

I hope this gives the idea?

Cor
 
Okay I understand what you mean and that actually solved another probelm..
but not the problem I asked about in this thread.

The problem is that if I use a DataView that has no records the
Autoincrement starts on 0 whereas the Access Tables (that the DataView is
based on) always start on 1... which is a bit of a problem. So when I add a
new row I see the ID field auto fills in a 0 but When I save, it becomes 1
in the database... and then all the other information in the application
based on that ID (which is 0 i runtime before I have saved) will be all
messed up. Cause their ParentID is then 0 which does'nt exist.

Maybe you misunderstood me OR maybe you actually answered me but I
misunderstod you?

I guess What I want is someway of telling the Dataview to start the
Autoincrement on 1 and not 0....

/Lars
 
Actually... If I use the DataTable as DataSource in the grid instead of the
DataView, the Autoincrement starts on 0 anyway... So it's not only the
DataView tha tis a problem for me..

Why does it start on 0 when Access 2000 starts on 1?

/Lars
 
What?

An Autoincrement cant' start on -1 and even if it could, it does'nt... it
starts on 0 in the DataView and the DataTable... and I want it to start on
1, since it's impossible to have a record in Access 2000 with an ID of 0 (if
it is an Autoincrement field)

/Lars
 
Wrong !

You misunderstand me.

The DataColumn class has an AutoIncrement Step which can be -1 and a See
which can be -1, therefore it is possible to allow the table to
Autoincrement in a negative direction. When Rows are insterted in the source
database, they can then take on new numbers, having the primary key set to a
negative index ensures that they never clash with existing numbers.

OHM
 
Okay, I have found where to set the Seed now.. but where in access can I
choose to use Negative Autoincrement?

/Lars
 
Hi Lars,

I have tried what you said, and it looks weird with the allownew = true.
I would not use that in this situation from you.

There is some behaviour that the fill after the update updates the dataset,
however does not things as deleting old rows that is probably as well
affected here. (I do not understand why however it seems to be)

That is not nice, therefore with those autoincrements, I think that you can
better do something as, (you would have to do that as well when you have
deletes in your dataset).
da.Update(ds)
ds.Clear()
da.Fill(ds)

I think the next is useless in this situation, however that is up to you,
setting the seed to 1 is:
ds.Tables(0).Columns("YourAutoIncKey").AutoIncrementSeed = 1

I hope this helps?

Cor
 
Lars,

Just make sure your ID is an Autonumber field in Access and all you have to
do Update, the dataAdapter will take care of it for you. You will find that
your new records have new sequential positive numbers in the Access DB.

Regards - OHM
 
Lars,

In the dataview you can set
dv.allownew = false

Then that automatic * row in the datagrid will not appear, when you add a
button to your form for a row and add with that to the datatable, then the
new row appears.

In your situation a row is created, which probably should not be there,
however than you cannot add rows, because that standard addrow * is not on
the datagrid.

Just my opinion however.

Cor
 
Lars,
In addition to the other's comments:

See the DataColumn.AutoIncrement, DataColumn.AutoIncrementSeed, and
DataColumn.AutoIncrementStep properties.

It is recommended that you make AutoIncrementStep & AutoIncrementSeed
both -1 on AutoIncrement columns so as to avoid conflicts between
autoincrement values assign by your Database (Access or SQL Server) and your
DataSet.

However you then need to have your update query return the newly assigned
AutoIncrement value when you update your database.

For a good tutorial on ADO.NET as well as a good desk reference once you
know ADO.NET see David Sceppa's book "Microsoft ADO.NET - Core Reference"
from MS press. David also shows how to use the -1 AutoIncrement in the
DataSet and have the database update the dataset (during DataSet.Update).

Hope this helps
Jay
 
see new thread "a VERY weird probelm when updating dataset" about my
problems after doing this and using Negative IDs

/lars
 
Back
Top