Getting next autoincremented id from typed DataTable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What's the alternative to the following?

void m_BindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
MyTypedDataSet.TypedTableRow newRow = this.m_DataTable.NewTypedTableRow();
this.m_DataTable = new MyTypedDataSet.VendorsDataTable();
DataRow[] existingRows = this.m_DataTable.Select("ID < 0", "ID ASC");
if (existingRows.Length > 0) {
object lowestRowID = existingRows[0]["ID"];
newRow.ID = ((int)lowestRowID) - 1;
}
else {
newRow.ID = -1;
}
e.NewObject = newRow;
}
 
You have at least two other options. You can retrieve the row from the
dataset by using the BindingSource.Position or you can cast the current row
to a datarowview and retrieve the value that way.

Dim CostID As Integer
Dim intRowPosition As Integer = YourBindingSource.Position
If intRowPosition > -1 Then
CostID = Convert.ToInt32(Ds.YourTable.Rows(intRowPosition)("CostID"))
End If

*************************************************************
Dim drView As DataRowView
'current gets the current item in the list -- thus no need for "position"
drView = CType(YourBindingSource.Current, DataRowView)
Dim CostID As Integer
CostID= Convert.ToInt32(drView.Row()("CostID"))

If you use BindingSource.Position, you must be careful that the row cannot
be sorted, otherwise you might be out of synch with your dataset. The
datarowview technique is more reliable.
 
RipT,

You can only get the real autoIncrement ID after adding the row real to the
datatabe. This can be a question of Nanoseconds. I assume that you are
working in a multiuser environment.

Cor
 
Cor,

Since the DataSet which contains the DataTables is local to the user's
instance of the client, I'm just trying to get a valid AutoIncrement ID for
the DataTable. The server-side database AutoIncrements from 1, with a +1
step; so, I've set the DataTable to AutoIncrement starting with -1, with a -1
step. Then when the DataSet is merged with the database, new rows will have
their negative IDs updated to valid positive IDs via the database manager's
AutoIncrement.

So I'm just trying to get the next valid negative ID from the DataTable
since I don't want to add invalid rows to the DataSet. The method I'm using
now gets the newest row from the DataTable and subtracts 1 from it's ID to
get the new ID. I am just wondering if the DataTable (or whatever object
which does the AutoIncrementing) either keeps track of the (last used or
next) AutoIncrement ID separately from the DataRows, or if it does a query on
insert to get the last used ID. Either way I'd like to take advantage of the
logic this object uses, if possible.

Thanks,
-rip

Cor Ligthert said:
RipT,

You can only get the real autoIncrement ID after adding the row real to the
datatabe. This can be a question of Nanoseconds. I assume that you are
working in a multiuser environment.

Cor


RipT said:
What's the alternative to the following?

void m_BindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
MyTypedDataSet.TypedTableRow newRow = this.m_DataTable.NewTypedTableRow();
this.m_DataTable = new MyTypedDataSet.VendorsDataTable();
DataRow[] existingRows = this.m_DataTable.Select("ID < 0", "ID ASC");
if (existingRows.Length > 0) {
object lowestRowID = existingRows[0]["ID"];
newRow.ID = ((int)lowestRowID) - 1;
}
else {
newRow.ID = -1;
}
e.NewObject = newRow;
}
 
Hi Rip

I think DataColumn.AutoIncrement, DataColumn.AutoIncrementSeed and
DataColumn.AutoIncrementStep are what you are looking for.

DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns["id"].AutoIncrement = true;
dt.Columns["id"].AutoIncrementSeed = -1;
dt.Columns["id"].AutoIncrementStep = -1;
DataRow dr = dt.NewRow();
Console.WriteLine(dr["id"]);

You can modify these properties in your .xsd file.

Hope this helps. Please let me if you have anything unclear.I'm glad to
assist you.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
WenYuan,

Thank you for your reply. Upon further testing I've found that my typed
DataSet had the fields you mentioned set incorrectly. Now everything is
working!

-rip
 
Back
Top