|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
newbie: why does DataSet AutoIncrement advance by 2?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; -- Show quoteMiha Markic [MVP C#] RightHand .NET consulting & development www.rthand.com Blog: http://cs.rthand.com/blogs/blog_with_righthand/ "deko" <deko@nospam.com> wrote in message news:kvidnfkWfuW2cZbZ4p2dnA@comcast.com... >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... And make sure you don't do call NewRow twice...
-- Show quote"Miha Markic [MVP C#]" <miha at rthand com> a écrit dans le message de news:exLhHyPQGHA.2628@TK2MSFTNGP15.phx.gbl... > 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" <deko@nospam.com> wrote in message > news:kvidnfkWfuW2cZbZ4p2dnA@comcast.com... > >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... > > > 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... -- Show quotePatrice "deko" <deko@nospam.com> a écrit dans le message de news:tLKdnbWSIpnJ0ZHZRVn-pg@comcast.com... > > 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(); You are 100% correct.> > 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... This also fixed the AutoIncrement double advance: dtP.Rows.Add(newRow); Thanks for the help! > You should use negative values for Auto*. This way they won't interfere Thanks for the tip.> with database generated values and you can immediately spot new row. > project_ID.AutoIncrementSeed = -1; > project_ID.AutoIncrementStep = -1; But what database generated code? 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 Show quote "deko" wrote: > > 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? > > > Database generated VALUES. The database is going to generate autonumber 10-4. But it was said (at least I think it was said) that it's better to > primary key values when you update the database with new rows from the > dataset. 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? |
|||||||||||||||||||||||