0

Cant seem to figure out how to check for a unique Employee id Number. I know the validation has to go in the form load, just not sure how to go about it.

Public Class Form1
  Dim filename As String
  Dim dataFile As System.IO.File
  Dim dataWrite As System.IO.StreamWriter

  ''LOADING AND WRITE TO TEXT DOCUMENT
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'asks user for file name 
    filename = InputBox("Enter output file name")
    If IO.File.Exists(filename) Then
      dataWrite = IO.File.AppendText(filename)
    Else
      MessageBox.Show("filename does not exist")
      filename = InputBox("Enter output file name")
      dataWrite = IO.File.CreateText(filename)
    End If
    cboDepart.Items.Add("Accounting")
    cboDepart.Items.Add("Administration")
    cboDepart.Items.Add("Marketing")
    cboDepart.Items.Add("MIS")
    cboDepart.Items.Add("Sales")
  End Sub
  '------

  Public EMPLOYEEIDS As String
  Dim employeeID1 As ServerData()
  Dim employeeID2 As ServerData()
  Dim reader As String = My.Computer.FileSystem.ReadAllText("servers.lst")
  Dim s() As String
  Dim Totalemployeeids As String = CStr(reader.Length)
  Dim x As Integer = 0
  Dim myArray As String() = reader.Split("|"c)
  For x = 1 To Totalemployeeids
    employeeID1(x).ServerName = myArray(0)
    employeeID2(x).IDname = myarray(0)

    Form1_load.ListBox1.Items.Add(Servers(x).ServerName)
    x += 1
  Next

  Structure ServerData
  End Structure
End Class
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
coderlisk
  • 11
  • 5
  • the only way to get a **unique** ID - without a database - is if you have all the other Emp IDs available. you dont really need it until you are saving a new Emp for the first time. I personally would not do all that IO in a form load. Test if `filename` is String.Empty the first time you go to save and prompt then. – Ňɏssa Pøngjǣrdenlarp Feb 20 '14 at 17:18

1 Answers1

0

You usually do not insert a unique ID from the client side. Instead, it is inserted automatically by the database server. There is a way to retrieve an inserted ID back, if you need it for display (can also act as a confirmation that a record was successfully inserted):

SELECT SCOPE_IDENTITY()

An example is shown in this answer:

On the client side, you need to implement insertion of everything but the ID, in this case you don't need to check for uniqueness. There may be other validation issues upon commit though (unique key violation, data type mismatch) - make sure you catch exceptions and display them to the user as appropriate.

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • i understand what you are saying. But going off what my teacher wants, when a user enters an employee ID number my program should be able to recognize whether or not the ID number has already been used and no longer can be entered. – coderlisk Feb 21 '14 at 19:01
  • further more copying straight off my assignment sheet. - "if a number being entered has already been entered, an error message should pop up and the user should be required to enter a unique employee number." – coderlisk Feb 21 '14 at 19:05
  • @coderlisk: Which is okay - if you properly catch the exception, it will tell you something like that (not sure about the exact wording). Try it. Regarding your last sentence, `user should be required to enter a unique employee number` <---- ok, stop here, do you have a database? – Victor Zakharov Feb 21 '14 at 19:58
  • theres no database .....yet! the way this class works its building off eachother so yes down the road it will have a database. but i can always change that out. so for now im looking for a quick statement to put in. which is what im having trouble with. what would i be using? or how would i write it rather? – coderlisk Feb 22 '14 at 01:42
  • @coderlisk: Depends on what you currently have. Name the memory structure, which holds a list of your employees. – Victor Zakharov Feb 22 '14 at 03:53
  • @coderlisk: Sure, go ahead and update your question. – Victor Zakharov Feb 22 '14 at 20:49
  • okay so im going to try the parse array thats what i got so far. any ideas on how it should look from there? @neolisk – coderlisk Feb 22 '14 at 21:13
  • @coderlisk: I edited your question to format the code for better readability. There is a problem - it won't compile as written. I think you missed some subs or functions. A class cannot contain bare code. Declarations are fine, i.e. `Public EMPLOYEEIDS As String`, but not this `For x = 1 To Totalemployeeids`. It is common to place all declarations on top (including a structure declaration) and all methods below that. What's inside `servers.lst` file? Please provide sample input (2-3 lines), no need to list all employees as part of this question. – Victor Zakharov Feb 22 '14 at 22:31