newbie: why does DataSet AutoIncrement advance by 2?

  • Thread starter Thread starter deko
  • Start date Start date
D

deko

I create a DataSet called database and add a table to it as shown below.
Why does the AutoIncrement value advance by 2 every time I add a record?
How do I get it to advance by 1?

DataTable tblProject = database.Tables.Add("TableProject");
DataColumn project_ID = tblProject.Columns.Add("Project_ID", typeof(Int32));
project_ID.AllowDBNull = false;
project_ID.Unique = true;
project_ID.AutoIncrement = true;
project_ID.AutoIncrementSeed = 1;
project_ID.AutoIncrementStep = 1;
tblProject.PrimaryKey = new DataColumn[] { tblProject.Columns[0] };

//other columns added here...
 
Hi,

You should use negative values for Auto*. This way they won't interfere with
database generated values and you can immediately spot new row.
project_ID.AutoIncrementSeed = -1;
project_ID.AutoIncrementStep = -1;
 
And make sure you don't do call NewRow twice...
--

Miha Markic said:
Hi,

You should use negative values for Auto*. This way they won't interfere with
database generated values and you can immediately spot new row.
project_ID.AutoIncrementSeed = -1;
project_ID.AutoIncrementStep = -1;


--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

deko said:
I create a DataSet called database and add a table to it as shown below.
Why does the AutoIncrement value advance by 2 every time I add a record?
How do I get it to advance by 1?

DataTable tblProject = database.Tables.Add("TableProject");
DataColumn project_ID = tblProject.Columns.Add("Project_ID",
typeof(Int32));
project_ID.AllowDBNull = false;
project_ID.Unique = true;
project_ID.AutoIncrement = true;
project_ID.AutoIncrementSeed = 1;
project_ID.AutoIncrementStep = 1;
tblProject.PrimaryKey = new DataColumn[] { tblProject.Columns[0] };

//other columns added here...
 
You should use negative values for Auto*. This way they won't interfere
with database generated values and you can immediately spot new row.
project_ID.AutoIncrementSeed = -1;
project_ID.AutoIncrementStep = -1;

Thanks for the tip.

But what database generated code?
 
And make sure you don't do call NewRow twice...

Here's how I add a row:

public int insertNewProject(string projectName)
{
dtP = database.Tables[tblP]; //tblP is a string const
int rowCount = dtP.Rows.Count;
Console.WriteLine(rowCount);
foreach (DataRow row in dtP.Rows)
{
Console.WriteLine(row[colPid]); //colP is a string const
}
DataRow newRow = dtP.NewRow();
Console.WriteLine(projectName);
newRow[colP] = projectName;
dtP.Rows.Add();
database.AcceptChanges();
DataRow lastRow = dtP.Rows[rowCount];
int pid = (int)lastRow[colPid]; //I want to get the ID of the newly added
row
return pid;
}

The AutoNumber PK still increments by 2 with the negative value, and
projectName never makes it into the table for some reason.
 
dtP.Rows.Add();

Never used this. It's likely adding a blank row. Try dtp.Rows.Add(newRow);
instead to add the row you previoulsy initialized rather than IMO a new
blank one...

--
Patrice

deko said:
And make sure you don't do call NewRow twice...

Here's how I add a row:

public int insertNewProject(string projectName)
{
dtP = database.Tables[tblP]; //tblP is a string const
int rowCount = dtP.Rows.Count;
Console.WriteLine(rowCount);
foreach (DataRow row in dtP.Rows)
{
Console.WriteLine(row[colPid]); //colP is a string const
}
DataRow newRow = dtP.NewRow();
Console.WriteLine(projectName);
newRow[colP] = projectName;
dtP.Rows.Add();
database.AcceptChanges();
DataRow lastRow = dtP.Rows[rowCount];
int pid = (int)lastRow[colPid]; //I want to get the ID of the newly added
row
return pid;
}

The AutoNumber PK still increments by 2 with the negative value, and
projectName never makes it into the table for some reason.
 
deko,

Database generated VALUES. The database is going to generate autonumber
primary key values when you update the database with new rows from the
dataset.

Kerry Moorman
 
dtP.Rows.Add();
Never used this. It's likely adding a blank row. Try dtp.Rows.Add(newRow);
instead to add the row you previoulsy initialized rather than IMO a new
blank one...

You are 100% correct.

This also fixed the AutoIncrement double advance:

dtP.Rows.Add(newRow);

Thanks for the help!
 
Database generated VALUES. The database is going to generate autonumber
primary key values when you update the database with new rows from the
dataset.

10-4. But it was said (at least I think it was said) that it's better to
step the AutoIncrement PK with -1 so it does not conflict with database
generated values. My question was WHAT database-generated values? Are we
talking about other AutoIncrement columns?

I could have a dozen AutoIncrement fields in the same table - there is no
conflict there.

Is stepping with -1 just a preference?
 
Back
Top