Click here to Skip to main content
14,937,132 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i am using vb form and SQL server 2014 my problem is i load data to datagridview
the when i loop the datagridview to insert max receipt number from database
so that each row in datagridiew have unique receipt number

For Each Row As DataGridViewRow In DataGridView1.SelectedRows
Dim i As Integer
Dim mixReciptNumber As Integer = ReceiptEntryTableAdapter1.ScalarQuery1(BILL")
For i = 0 To mixReciptNumber + 1
ReciptNumber = mixReciptNumber + 1

'Me.ReceiptEntryTableAdapter1.InsertQuery(ReciptNumber, 0, DateTimePicker1.Text, DateTimePicker1.Text, Row.Cells(1).Value, Row.Cells(2).Value, Row.Cells(3).Value, "0", "Group", Row.Cells(6).Value, Row.Cells(7).Value, Row.Cells(8).Value, Row.Cells(9).Value, "0", "0", "0")
Updated 25-Apr-15 22:56pm
CHill60 25-Apr-15 11:05am
It's not at all clear what your problem is.

For the unique number - why not just have an identity column on the database table?
Maciej Los 26-Apr-15 4:56am
What next is doing there twice?
OriginalGriff 26-Apr-15 5:25am
Nested loops.
Maciej Los 26-Apr-15 5:47am
Ohhh.... i see it now ;)

1 solution

If i understand you correctly you want to increase ReciptNumber for each selected row in DataFridView. So, you need only one loop:
Dim ReciptNumber As Integer = 0
Dim mixReciptNumber As Integer = ReceiptEntryTableAdapter1.ScalarQuery1("BILL")
ReciptNumber = mixReciptNumber 
For Each Row As DataGridViewRow In DataGridView1.SelectedRows
    ReciptNumber = mixReciptNumber + 1
    'insert part

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900