Auto Number Generator
-
PeriodAdapter.Fill(DataSet11, "period") Dim datatable As DataTable = DataSet11.Tables("Period") Dim dr As DataRow = datatable.NewRow Dim r() As DataRow = datatable.Select("periodno= max(periodno)") Dim i As Integer For i = 0 To r.Length - 1 dr("periodno") = CInt(r(i)("periodno")) + 1 dr("begindate") = DateTimePicker1.Value.Date dr("enddate") = DateTimePicker2.Value.Date datatable.Rows.Add(dr) Next MsgBox("New period has been saved", MsgBoxStyle.Information, "New Period") PeriodAdapter.Update(DataSet11, "period") -------------------------- Well i havent search from internet about this fucntion yet and above code is just what i modify a bit and use for generate new id or number to my record by adding 1. The problem is if there are not records at database, above code cannot add the first record let said periodno =1 ........... and keep giving blank record when i see at my database. if i manually insert periodno=1 then above code can give the next periodno=2. How to make condition if my database is empty, i tried use Nothing syntax also cannot. What should i do....
-
PeriodAdapter.Fill(DataSet11, "period") Dim datatable As DataTable = DataSet11.Tables("Period") Dim dr As DataRow = datatable.NewRow Dim r() As DataRow = datatable.Select("periodno= max(periodno)") Dim i As Integer For i = 0 To r.Length - 1 dr("periodno") = CInt(r(i)("periodno")) + 1 dr("begindate") = DateTimePicker1.Value.Date dr("enddate") = DateTimePicker2.Value.Date datatable.Rows.Add(dr) Next MsgBox("New period has been saved", MsgBoxStyle.Information, "New Period") PeriodAdapter.Update(DataSet11, "period") -------------------------- Well i havent search from internet about this fucntion yet and above code is just what i modify a bit and use for generate new id or number to my record by adding 1. The problem is if there are not records at database, above code cannot add the first record let said periodno =1 ........... and keep giving blank record when i see at my database. if i manually insert periodno=1 then above code can give the next periodno=2. How to make condition if my database is empty, i tried use Nothing syntax also cannot. What should i do....
Hi, first of all: If your periodno is unique you won't need the loop. To your question: If you don't have any entries in the Period table the Select function will return an empty array:
PeriodAdapter.Fill(DataSet11, "period")
Dim datatable As DataTable = DataSet11.Tables("Period")
Dim dr As DataRow = datatable.NewRowDim r() As DataRow = datatable.Select("periodno= max(periodno)")
Dim nextPeriodNo as Integer
If (r.Length == 0) Then
nextPeriodNo = 1
Else
nextPeriodNo = CInt(r(i)("periodno")) + 1
End If
Dim i As Integerdr("periodno") = nextPeriodNo
dr("begindate") = DateTimePicker1.Value.Date
dr("enddate") = DateTimePicker2.Value.Date
datatable.Rows.Add(dr)MsgBox("New period has been saved", MsgBoxStyle.Information, "New Period")
PeriodAdapter.Update(DataSet11, "period")