Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm stuck with what I'm sure, is a very simple problem.

I'm trying to save an employee's login time, login date, and logout time into a database. So far what I'm doing is, when the employee first logs in it saves their login time and date, but obviously not their logout time since they haven't logged out yet. Next when the main form is closing, it needs to record their logout time and update their existing record to complete it.

Okay so what I have so far is:

It will record the login time and login date correctly.

And it will record the logout time correctly and I have the code necessary to record it into an existing record.

But, my main problem is how do I pull back up that existing record in the first place?

While creating the new login record I tried to use a variable to save the LoginDataID so it can be passed onto the main form to bring the LoginDataRecord up by its ID when closing. This part, I can't seem to get to work.

Basically, what I'm asking is will anyone help me find a way to capture the ID of the new LoginDataRecord that is created when the employee first logs in, so it can be used when I query the LoginDataRecord, using LINQ, to find their LoginDataRecord and update it with the logout time when the main form is closing?

Also at the moment, I'm strictly a Visual Basic programmer. I'm still in college, and my skills with Java and C# are very, very basic and I can't really do much in them.

Anyways, thanks for any and all help.

Here's the code for the Login Form.
VB
Imports NEIPurchasingDAL.PurchasingDatabaseEntities
Imports System.Data.Objects

Public Class LoginForm

    Private PE As NEIPurchasingDAL.PurchasingDatabaseEntities = New NEIPurchasingDAL.PurchasingDatabaseEntities
    Public LoginID As String
     
    Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click

        Dim MyUser As String = My.User.Name
        Dim LoginDate As String = Today.ToShortDateString
        Dim LoginTime As String = Now.ToLongTimeString

        Dim MyUserName As String = Replace(MyUser, "NEIHERRIN\", "")

        MyUserName = My.User.Name

        Dim LoginDataRecord As New NEIPurchasingDAL.LoginData

        With LoginDataRecord
            .Username = MyUserName
            .LoginTime = LoginTime
            .LoginDate = LoginDate
        End With

        PE.AddToLoginDatas(LoginDataRecord)
	
        LoginID = LoginDataRecord.LoginID

        PE.SaveChanges()

        
        MainForm.Show()
        Me.Visible = False


    End Sub
   
End Class


And here's the code for the Main Form
VB
Imports NEIPurchasingDAL.PurchasingDatabaseEntities
Imports System.Data.Objects

Public Class MainForm

    Private PE As NEIPurchasingDAL.PurchasingDatabaseEntities = New NEIPurchasingDAL.PurchasingDatabaseEntities
    Private MyUser As String = My.User.Name

    Private Sub MainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles_ Me.FormClosing

        Dim LogoutTime As String = Now.ToLongTimeString

        Try
            Dim LoginData As NEIPurchasingDAL.LoginData

            LoginData = (From LoginDatas In PE.LoginDatas
                         Where LoginDatas.LoginID = LoginForm.LoginID
                        Select LoginDatas)


            LoginData.LogoutTime = LogoutTime

            PE.SaveChanges()

        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try

        LoginForm.Visible = True

    End Sub
End Class


The problem is that I can't get the Public variable LoginID to store the LoginID on the actual entity and then use it for comparison on the Main Form. Assuming it is possible.
Posted
Updated 9-Mar-12 5:39am
v3
Comments
Herman<T>.Instance 9-Mar-12 10:45am    
is the ID field in your DB an Identity field?
Mr.McCloud 9-Mar-12 11:07am    
Yeah
PopeDarren 9-Mar-12 11:39am    
You have to reference the ID after you savechanges. Put it below the PE.SaveChanges() and put a breakpoint on it. When you hit the breakpoint, mouse over the LoginID and it will show you the value. If that's true, then it's a matter of managing the variable correctly so you have it when you log out.
Mr.McCloud 9-Mar-12 11:44am    
Wow I can't believe I made such a simple mistake. That did the trick. Thanks a ton for the help and a ton of thanks to Mark Nischalke too. By the way, it seems I can't make a comment as a solution. Can you retype this comment as the solution to my problem?
PopeDarren 9-Mar-12 11:47am    
My solution is solution 2 at the bottom below your comments on solution 1. ;) I figured it was missed! Good luck, and good work using entities! Keep it up!

VB
LoginID = LoginDataRecord.LoginID
PE.SaveChanges()


Reverse these lines. The ID will not be set until the database has been updated by the SubmitChanges method.
 
Share this answer
 
AFTER you do a SubmitChanges() or SaveChanges(), you can just reference the ID field.

LoginDataRecord.LoginID

And that will retrieve your ID, but you have to reference this after the changes have been made.
 
Share this answer
 
v5
Comments
[no name] 9-Mar-12 12:16pm    
It is more common to use the SCOPE_IDENTITY method to return the identity rather than using OUTPUT.

There is no code changes required with LINQ. The identity field for the object will be updated via the context after it has been committed
PopeDarren 9-Mar-12 12:26pm    
We established that he wasn't using a stored procedure. I just didn't edit my original answer to remove that part. You're the codeProject guru it looks like! Should I edit my solution to only meet Mr.McCloud's needs? Thanks for your response!

SCOPE_IDENTITY may be more common, but I would prefer to send anyone who may be looking for a way to output a variable down a safer path.

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/f7c8b3ee-1b03-46c6-a446-af87a6a2f5ea

Scope_Identity may be better than @@identity, but it still has problems:

http://connect.microsoft.com/SQLServer/feedback/details/328811/scope-identity-sometimes-returns-incorrect-value
[no name] 9-Mar-12 12:38pm    
You refer to old issues that have been corrected and a version of SQL that has only been released a few days ago and not in use yet.

Yes, you should edit your solution to make it accurate. People in the future may be searching for a solution and see your incorrect or incomplete one without reading all of the comments.
PopeDarren 9-Mar-12 12:41pm    
They may be old issues to Microsoft, but not every IT shop is up to date. OUTPUT remains the safest option in my opinion. But you're probably right, SCOPE_IDENTITY is probably more common.

Excellent point. I will update as you suggest. Thanks again.
I would design the database tables so the userid is a primary key on the table. A composite key with an autonumber would work. This way it will be much easier to retrieve data for a particular user and update appropriately. At login you add a record, at logout you retrieve the last record entered for the user and update it.

It's not a matter of which language you are best at it's simply the logic and architecture.
 
Share this answer
 
Comments
Mr.McCloud 9-Mar-12 11:09am    
Yeah, that's basically what I'm trying to do. I just don't know the code that will allow me to do this. Finding Entity Framework code in Visual Basic online can be very difficult.
[no name] 9-Mar-12 11:18am    
Again, it isn't about the language. LINQ code is the same in all languages.

Not accurate all but you would do something like this

from u in Users
where u.UserId == userid and Id == Max(Id)
select u
[no name] 9-Mar-12 11:32am    
Don't add code as a comment. Edit your post to include any updates and properly format it.

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