DataTable.Columns(PKColumn).AutoIncrementSeed = -1 RRS feed

  • Question

  • Hi Guys, 
    I'm trying to set the AutoIncrement Seed to -1, so when I add new data to the datatable my Auto Number starts at -1, -2, -3 etc...
    I believe I did the set-up correctly, however it's not working the way I expected it.
    So when I try to add new record it's taking the Max Auto number and subtract 1 from it. I didn't want this behavior I just want to start by -1 for new record. 

    Am I missing some property that I need to set?

    See Code Below

    Thank you

                Using Adapter As SqlDataAdapter = _  

                    New SqlDataAdapter("myStoredProcedure", My.Settings.myConnection)  

                    Adapter.SelectCommand.CommandType = CommandType.StoredProcedure  

                    'Get the Schema of the Table along with the data  

                    Adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey  

                    Adapter.Fill(ds, "TableName")  


                    dt = ds.Tables("TableName")  

                    'Get Fields Ordinal  

                    IDField = dt.CreateDataReader.GetOrdinal("IDField")  

                    DescriptionField = dt.CreateDataReader.GetOrdinal("DescriptionField")  


                    With dt.Columns(IDField)  

                        .AutoIncrement = True 

                        .AutoIncrementSeed = -1  

                        .AutoIncrementStep = -1  

                    End With 

                    myDataGridView.DataSource = dt  


                End Using 

    Friday, March 6, 2009 6:22 PM


  • I figured it out, because I was bringing the schema of sql server table along with the data, I couldn't override the AutoNumber Increment.

    I had to take this code out
    Adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey 

    • Marked as answer by Codernater Friday, March 6, 2009 6:38 PM
    Friday, March 6, 2009 6:37 PM