Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I began creating a click event in which I hope to
1. match the employee first and last name and (employee ID),
2. return a message to the page if the employee already exists,
3. I also want to display the next available employee ID in a textbox.
The user wants the ID to show.

I spent many hours trying to correctly write the code to display the ID and messages appropriately. The following is the portion that is syntactically correct:

I would appreciate any help you can give me in writing this correctly.

VB
Protected Sub btnUpdate2_Click(ByVal sender As Object, ByVal e As EventArgs) 
        Handles btnUpdate2.Click
        Using context As New personnelDataContext()
            Dim employees = From c In context.Employees _
                Where (c.lastName.Contains(txtLastName.Text.Trim())
                 AndAlso c.firstName.Contains(txtFirstName.Text.Trim())) _
                Select c

            If (employees Is Nothing) Then
                'there was no matching employee in the database
                Dim employee As New Employee()
                context.Employees.InsertOnSubmit(employee)
                context.SubmitChanges()
            End If
        End Using
    End Sub
Posted
Updated 24-Mar-10 8:10am
v2

And what are you having difficulty with?

if(employee == null)
  // add new 
  // display employee.ID
else
  // display message
 
Share this answer
 
The employees variable will not be Nothing if there was no matching employee in the database. Rather, it will be an empty collection.

Change
VB
If (employees Is Nothing) Then
    'there was no matching employee in the database
    Dim employee As New Employee()
    context.Employees.InsertOnSubmit(employee)
    context.SubmitChanges()
End If

to
VB
If (employees Is Nothing OrElse employees.Count() < 1) Then
    'there was no matching employee in the database
    Dim employee As New Employee()
    context.Employees.InsertOnSubmit(employee)
    context.SubmitChanges()
End If


You should also check whether or not the string comparisons (lastName.Contains() and firstName.Contains()) you use in your query should be case-sensitive. Right now I believe they are.

For additional help, you'll need to clarify exactly what problems you're having.
 
Share this answer
 
Thank you very much for the responses. I'm having trouble with the syntax of displaying the next available employee_id is a text box.

I wrote the stored procedure with employee_id as an output. Just want to show it in the text box when the update button is clicked.
 
Share this answer
 

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