Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Auto Number Generator

Auto Number Generator

Scheduled Pinned Locked Moved Visual Basic
databasehelptutorialannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LordLothar
    wrote on last edited by
    #1

    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....

    R 1 Reply Last reply
    0
    • L LordLothar

      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....

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      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.NewRow

      Dim 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 Integer

      dr("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")

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups