• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Insert/Update

With the code below what would be the best way to check to see if "Username" is allready in the database... if it is updateit if not insert it?

  Public Shared Sub trackusers(ByVal thepage As String)

        Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb")
            Dim Username As String = Membership.GetUser.UserName.ToString
            Using cmd As New OleDbCommand("INSERT into nousersonline (Username,Activity,PageUrl,ActivityDate) VALUES ('" & Username & "','', '" & thepage & "','" & DateAndTime.Now.ToString & "')", con)
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    End Sub

Open in new window

0
runnerjp2005
Asked:
runnerjp2005
1 Solution
 
CodeCruiserCommented:
You can use a stored procedure. Or to do it in code, try this example




Public Shared Sub trackusers(ByVal thepage As String)

        Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb")
            Dim Username As String = Membership.GetUser.UserName.ToString
            Using cmd As New OleDbCommand("Select Count(UserID) From tbl... Where UserName='" & UserName & "'", con)
                con.Open()
                Dim cnt As Integer = cmd.ExecuteScalar()
                If cnt = 0 Then
                   cmd.CommandText = "INSERT into nousersonline (Username,Activity,PageUrl,ActivityDate) VALUES ('" & Username & "','', '" & thepage & "','" & DateAndTime.Now.ToString & "')"
                Else
                   cmd.CommandText = "Update ..."
                End If
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    End Sub

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now