Imports System.Data.Sql
Imports System.Data.SqlClient
Partial Class Vote
Inherits System.Web.UI.Page
Function hasvalues() As Boolean
Dim currentUser As MembershipUser = Membership.GetUser()
Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)
Dim retvalue As Boolean = False
Dim connectionString As String = ConfigurationManager.ConnectionStrings("SecurityTutorialsConnectionString").ConnectionString
Dim insertSql As String = "SELECT COUNT(*) FROM Awards WHERE UserId = @UserId"
Using myConnection As New SqlConnection(connectionString)
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@UserId", currentUserId)
Try
myConnection.Open()
Dim val = mycommand.executescalar()
If val IsNot Nothing Then
If int32.parse(val) > 0 Then
retvalue = True
End If
End If
myConnection.Close()
Catch ex As Exception
Throw ex
End Try
End Using
Return retvalue
End Function
Protected Sub PostAward_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PostAward.Click
Dim currentUser As MembershipUser = Membership.GetUser()
Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)
Dim connectionString As String = ConfigurationManager.ConnectionStrings("SecurityTutorialsConnectionString").ConnectionString
Dim insertSql As String = "INSERT INTO Awards (Award1, Award2, Award3, Award4, Award5, Award6, Award7, Award8, UserId) VALUES (@Award1, @Award2, @Award3, @Award4, @Award5, @Award6, @Award7, @Award8, @UserId)"
'Check if user has already voted
If hasvoted() Then
lblmessage.Text = "You cannot vote more than once!"
Exit Sub ' You can either do this Exit Sub, or do a Redirect
End If
Try
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@Award1", Award1.Text.Trim())
myCommand.Parameters.AddWithValue("@Award2", Award2.Text.Trim())
myCommand.Parameters.AddWithValue("@Award3", Award3.Text.Trim())
myCommand.Parameters.AddWithValue("@Award4", Award4.Text.Trim())
myCommand.Parameters.AddWithValue("@Award5", Award5.Text.Trim())
myCommand.Parameters.AddWithValue("@Award6", Award6.Text.Trim())
myCommand.Parameters.AddWithValue("@Award7", Award7.Text.Trim())
myCommand.Parameters.AddWithValue("@Award8", Award8.Text.Trim())
myCommand.Parameters.AddWithValue("@UserId", currentUserId)
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
Response.Redirect("Default.aspx", False)
Catch ex As Exception
Throw ex
End Try
End Sub
Function hasvoted() As Boolean
'code to check if that user id already has record and return True or False
End Function
End Class
And error codes = Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.Awards'.
The statement has been terminated. again, the original exception has come back. There is no warning message at the top of the screen when I go to the page.
That's what he's talking about now. You either need to not allow them to click submit (querying before main page comes with the hasvalues() function), or figure out after the button is pressed (what you have now).
in page_load(and change btnsubmit to correct button id from your aspx page)
protected sub page_load(byval sender as object, byval e as eventargs) handles me.load
if hasvoted() then
lblMessage.text = "you already registered your voted!"
btnsubmit.enabled = false
else
btnsubmit.enabled = true
end if
end sub
hoodedperson...
Star
12950 Points
3196 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:01 PM|LINK
Yeah, I just tried to get you some code, so there are some things better in rsw20's code. Try either! haha
tottenham_ru...
Member
11 Points
87 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:03 PM|LINK
So I put the function and the other part together?
There is still lots of errors however.
rsw20
Member
548 Points
112 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:10 PM|LINK
post errors and your complete code please
tottenham_ru...
Member
11 Points
87 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:16 PM|LINK
Complete code:
Imports System.Data.Sql Imports System.Data.SqlClient Partial Class Vote Inherits System.Web.UI.Page Function hasvalues() As Boolean Dim currentUser As MembershipUser = Membership.GetUser() Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid) Dim retvalue As Boolean = False Dim connectionString As String = ConfigurationManager.ConnectionStrings("SecurityTutorialsConnectionString").ConnectionString Dim insertSql As String = "SELECT COUNT(*) FROM Awards WHERE UserId = @UserId" Using myConnection As New SqlConnection(connectionString) Dim myCommand As New SqlCommand(insertSql, myConnection) myCommand.Parameters.AddWithValue("@UserId", currentUserId) Try myConnection.Open() Dim val = mycommand.executescalar() If val IsNot Nothing Then If int32.parse(val) > 0 Then retvalue = True End If End If myConnection.Close() Catch ex As Exception Throw ex End Try End Using Return retvalue End Function Protected Sub PostAward_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PostAward.Click Dim currentUser As MembershipUser = Membership.GetUser() Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid) Dim connectionString As String = ConfigurationManager.ConnectionStrings("SecurityTutorialsConnectionString").ConnectionString Dim insertSql As String = "INSERT INTO Awards (Award1, Award2, Award3, Award4, Award5, Award6, Award7, Award8, UserId) VALUES (@Award1, @Award2, @Award3, @Award4, @Award5, @Award6, @Award7, @Award8, @UserId)" 'Check if user has already voted If hasvoted() Then lblmessage.Text = "You cannot vote more than once!" Exit Sub ' You can either do this Exit Sub, or do a Redirect End If Try Using myConnection As New SqlConnection(connectionString) myConnection.Open() Dim myCommand As New SqlCommand(insertSql, myConnection) myCommand.Parameters.AddWithValue("@Award1", Award1.Text.Trim()) myCommand.Parameters.AddWithValue("@Award2", Award2.Text.Trim()) myCommand.Parameters.AddWithValue("@Award3", Award3.Text.Trim()) myCommand.Parameters.AddWithValue("@Award4", Award4.Text.Trim()) myCommand.Parameters.AddWithValue("@Award5", Award5.Text.Trim()) myCommand.Parameters.AddWithValue("@Award6", Award6.Text.Trim()) myCommand.Parameters.AddWithValue("@Award7", Award7.Text.Trim()) myCommand.Parameters.AddWithValue("@Award8", Award8.Text.Trim()) myCommand.Parameters.AddWithValue("@UserId", currentUserId) myCommand.ExecuteNonQuery() myConnection.Close() End Using Response.Redirect("Default.aspx", False) Catch ex As Exception Throw ex End Try End Sub Function hasvoted() As Boolean 'code to check if that user id already has record and return True or False End Function End ClassAnd error codes = Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.Awards'.
The statement has been terminated. again, the original exception has come back. There is no warning message at the top of the screen when I go to the page.
rsw20
Member
548 Points
112 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:19 PM|LINK
1. rename hasvalues() to hasvoted()
2. remove this code
Function hasvoted() As Boolean
'code to check if that user id already has record and return True or False
End Function
tottenham_ru...
Member
11 Points
87 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:26 PM|LINK
It worked! Is this code meant to say "You can't vote twice" before you click on the submit button, or after you press submit?
rsw20
Member
548 Points
112 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:28 PM|LINK
after you click
or
you can customize code to disable submit button on pageload if user has already submitted vote.
This would require some change to existing code (let me know if you want that)
tottenham_ru...
Member
11 Points
87 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:30 PM|LINK
Ok, thanks! Is it possible to display the message before we click submit?
hoodedperson...
Star
12950 Points
3196 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:38 PM|LINK
That's what he's talking about now. You either need to not allow them to click submit (querying before main page comes with the hasvalues() function), or figure out after the button is pressed (what you have now).
rsw20
Member
548 Points
112 Posts
Re: Violation of PRIMARY KEY constraint 'PK_Awards'. Cannot insert duplicate key in object 'dbo.A...
Jun 02, 2010 09:39 PM|LINK
yes
in page_load(and change btnsubmit to correct button id from your aspx page)