Greetings once more experts.
I have the code below that saves user's vote on the database.
When a user's record is submitted for the first time, it submits successfully and displays message that user's submission is successfully.
When a user attempts to submit the votes again, using same username, the votes do not get submitted which is exactly the way it is supposed to work.
However, the same message which says submission is successful gets displayed again and this is wrong.
We would like to show success message when user submits record for the first time.
If, however, the user attempts to submit same records again, submission should fail as it does currently but the user should see a message that says, "You have already submitted your votes!"
Can you please help me understand why the code is not doing that?
Thanks in advance as always
CREATE TABLE [dbo].[SurveyAnswers](
[uid] [int] IDENTITY(1,1) NOT NULL,
[positionID] [int] NULL,
[ChoiceID] [int] NULL,
[ChoiceText] [nvarchar](50) NULL,
[UserName] [nvarchar](50) NULL,
[dateTaken] [datetime] NULL
) ON [PRIMARY]
GO
Private Sub SaveVotes(ByVal qid As Integer, ByVal cid As Integer, ByVal ct As String, ByVal cuser As String)
Dim al As ArrayList = CType(Session("AnswerList"), ArrayList)
Dim connStr As String = ConfigurationManager.ConnectionStrings("BallotsConnectionString").ConnectionString
Dim conn As New SqlConnection(connStr)
Dim cmd As New SqlCommand("IF NOT EXISTS (SELECT positionid FROM SurveyAnswers WHERE positionid = @qid and UserName = @cuser) " &
" INSERT INTO SurveyAnswers(positionId,ChoiceID,ChoiceText,Username,dateTaken) values(@qid,@cid,@ct,@cuser,getDate())", conn)
cmd.CommandType = CommandType.Text
Dim p1 As New SqlParameter("@qid", qid)
Dim p2 As New SqlParameter("@cid", IIf(cid = 0, DBNull.Value, cid))
Dim p3 As New SqlParameter("@ct", IIf(ct = "", DBNull.Value, ct))
Dim p4 As New SqlParameter("@cuser", Session("UserName"))
cmd.Parameters.Add(p1)
cmd.Parameters.Add(p2)
cmd.Parameters.Add(p3)
cmd.Parameters.Add(p4)
Try
conn.Open()
cmd.ExecuteNonQuery()
lblMsg.ForeColor = Color.Green
lblMsg.Text = "Thank you for participating in this election!.Please close your browse to exit the system."
Catch Ex As Exception
lblMsg.Text = "You have already submitted your votes!"
lblMsg.ForeColor = Color.Red
Finally
conn.Close()
End Try
End Sub