Ok thanks guys. With regards to your question hooded. Ideally the user wont be redirected to the homepage if they have already voted, instead they return to the voting page with an error message.
Try
myCommand.ExecuteNonQuery()
Catch ex As Exception
lblMessage.Text = "You have already voted, you can't vote twice. These new votes, have not been registered." 'use .text to display
End Try
myConnection.Close()
End Using
'...
Catch ex As Exception
lblMessage.Text = 'blah blah
Dim error As Boolean = True
End Try
If(error <> True)
Response.Redirect("whatever page you had here")
End If
'(otherwise, the lblMessage.Text is changed to blah blah, and the page is not redirected.
That does work, hooded, but then I get the problem of the error message conflicting (logically) with the other content on the page. So would it be possible to have a pop up box with the error message, and on the box is a button saying continue, to "Home"
(default.aspx)?
Or is that not the problem you mean? Sorry, I didn't understand what you meant. Well, you can do a MessageBox, but I've heard they don't work after you upload your project to a server...I'm not sure why.
I see what you mean...well if these other things don't end up working, we can advise to change to your checking if voted first. I just didn't want to have them change (a lot) of code when they went with my seemingly helpful code
Well I made a message with an error sound number "48". But if it doesn't work when I upload it to the server then there's no point in doing it!!
So would you recommend doing a check at the very beginning of the page load, before they even press the submit button? Sounds like a good idea, but I feel it's unfair if I make you explain more code, so don't worry bout it.
Thanks,
Mike
P.S. any other hosting recommendations? The pixel one was good but charge SQL by the year, and I only need it for 2 months!
Hah it's no problem at all! Well, I meant only a MessageBox isn't supposed to work for some reason, so you could've kept with the lblMessage.Text = "" and stuff, but maybe it is better to do this (from earlier poster's code):
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
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
I changed it some, and changed tiny things, so don't miss them! Let me know what you think/if it works (if you try it)! :)
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 07:05 PM|LINK
yes, say some unexpected error occured, column name changed, table name changed or anyother sql exception it will still display same message
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 07:07 PM|LINK
Ok thanks guys. With regards to your question hooded. Ideally the user wont be redirected to the homepage if they have already voted, instead they return to the voting page with an error message.
Try myCommand.ExecuteNonQuery() Catch ex As Exception lblMessage.Text = "You have already voted, you can't vote twice. These new votes, have not been registered." 'use .text to display End Try myConnection.Close() End UsingHowever, it says lblMessage was not declared.
Thanks,
Mike
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 07:13 PM|LINK
add to aspx page
<asp:Label id="lblMessage" runat="server" />
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 07:14 PM|LINK
Okay, then you might want to change to:
'... Catch ex As Exception lblMessage.Text = 'blah blah Dim error As Boolean = True End Try If(error <> True) Response.Redirect("whatever page you had here") End If '(otherwise, the lblMessage.Text is changed to blah blah, and the page is not redirected.Does this work?
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 07:20 PM|LINK
That does work, hooded, but then I get the problem of the error message conflicting (logically) with the other content on the page. So would it be possible to have a pop up box with the error message, and on the box is a button saying continue, to "Home" (default.aspx)?
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 07:36 PM|LINK
Can you add this code from rsw20?
Or is that not the problem you mean? Sorry, I didn't understand what you meant. Well, you can do a MessageBox, but I've heard they don't work after you upload your project to a server...I'm not sure why.
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 07:38 PM|LINK
I see what you mean...well if these other things don't end up working, we can advise to change to your checking if voted first. I just didn't want to have them change (a lot) of code when they went with my seemingly helpful code
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 07:52 PM|LINK
if you want to display popup box you have to use use javascript to display that message.
with <Asp:label> you can display message on page but not as popup
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 07:56 PM|LINK
Well I made a message with an error sound number "48". But if it doesn't work when I upload it to the server then there's no point in doing it!!
So would you recommend doing a check at the very beginning of the page load, before they even press the submit button? Sounds like a good idea, but I feel it's unfair if I make you explain more code, so don't worry bout it.
Thanks,
Mike
P.S. any other hosting recommendations? The pixel one was good but charge SQL by the year, and I only need it for 2 months!
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 08:08 PM|LINK
Hah it's no problem at all! Well, I meant only a MessageBox isn't supposed to work for some reason, so you could've kept with the lblMessage.Text = "" and stuff, but maybe it is better to do this (from earlier poster's code):
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 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 FunctionI changed it some, and changed tiny things, so don't miss them! Let me know what you think/if it works (if you try it)! :)