Increment value in a dataset

keith

New member
Joined
Dec 6, 2004
Messages
3
Programming Experience
1-3
Hi I'm a beginner at VB .net.
I have a table orderNumber containing 1 column orderNo. There is only one value in this column. I want to increment its value by 1 everytime a user clicks on a button. I have the table in a dataset but cannot increment the value, which is an integer. I appreciate any help given. Thanks.
 
Last edited:

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
Can You Supply More Info?

What Type Of Database Are You Using. Is The Field Set As Autoincrement At The Database Level? Is The Field Set As The Primary Key? Are You Incrementing An Existing Record Or Adding The Next Record In Sequence? Are you displaying the records in a grid or textbox?
There Is A Simple Solution (i'm Sure) But It Requires More Information And Code Samples Are Always Helpful.

the simple grid based answer is:
Dim iCurrentOrderNum As Integer
'set the current value
iCurrentOrderNum = OrderGrid.Columns("order number").Value
'increment the value
iCurrentOrderNum += 1
'reset the grid value
OrderGrid.Columns("order number").Value = iCurrentOrderNum
 
Last edited:

keith

New member
Joined
Dec 6, 2004
Messages
3
Programming Experience
1-3
Its an SQL Server database.
I dont understand what autoincrement does, but I do have it enabled.
The field is the primary key
It is an existing record I want to increment.
I'm not displaying it, I am updating the database.
My problem basically is I cannot assign the value from the database to a local variable. I could then increment it and update the database????
Thanks
 

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
Sounds like you have the field set as Identity in SQL Server. You can not increment it while it is set as Identity. You need to turn off Identity in server manager, you can leave it as a primary key, to be able to increment it.
 

keith

New member
Joined
Dec 6, 2004
Messages
3
Programming Experience
1-3
Identity is set to No. Identity increment is blanked out.

How do I reference the field in the dataset?

Just to let you know what I'm doing: The table only contains one number. It is used to give each order a unique number. So everytime I start an order for a customer it will read this number, put it in the order table (a different table), increment it and update the database. with the new orderNo.
 
Last edited:

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
Ok, I do about the same thing with an Operations Table and increment new records by 10.
I look at the last record in the Operations table and increment that operation number by 10 when a new record is added.
This is done on a grid form. I use the values displayed in the grid.

here is my code:

public iopnumber As Integer
public iCurrentPartid As Integer


'go to last Operation Record
Me.BindingContext(DsOperations1, "Operations").Position = (DsOperations1.Tables("Operations").Rows.Count - 1)
Try
'increment the operation number variable (Last Op Number + 10)
iopnumber = OperationsGrid1.Columns("operation number").Value + 10
Catch ex As Exception
iopnumber = 10
End Try
Dim tblOperations As DataTable
tblOperations = DsOperations1.Tables("operations")
Dim drCurrentop As DataRow
drCurrentop = tblOperations.NewRow()
' Set the DataRow field values as necessary.
drCurrentop("Part ID") = iCurrentPartid
drCurrentop("operation number") = iopnumber
tblOperations.Rows.Add(drCurrentop)

Try
'Attempt to update and load the dataset.
Me.UpdateDataSet()
Me.LoadDataSet()
Catch eLoad As System.Exception
'Add your error handling code here.
'Display error message, if any.
System.Windows.Forms.MessageBox.Show(eLoad.Message)
End Try
 
Last edited:
Top Bottom