Seven price
asked on
voting poll
I have a poll when I test it. no matter what option I select it always votes value 1. not sure whats wrong. and when I add the cookie to record the vote it does not vote at all. But for now I just need to know why it only votes for value one.
stored procedure
(
@Pk_OptionId INT
)
AS
-- GETS THE CURRENT NUMBER OF VOTES FOR THAT OPTION
DECLARE @i_NumberOfVoteS INT
SELECT @i_NumberOfVotes = Votes
FROM PollOptions
WHERE PK_OptionId = @Pk_OptionId
UPDATE PollOptions
SET Votes = (@i_NumberOfVotes + 1)
WHERE PK_OptionId = @Pk_OptionId
RETURN
Protected Sub btnVote_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVote.Click
RecordVote()
'If Response.Cookies("Voted") Is Nothing Then
' Response.Cookies("Voted"). Value = "Voted"
' Response.Cookies("Voted"). Expires = DateTime.Now.AddDays(1)
' lblError.Visible = False
' ' Checks if the user can still vote by using cookie
' RecordVote()
'Else
' lblError.Visible = True
'End If
'Response.Write(Response.C ookies("Vo ted"))
'' Response.Redirect("test.as px")
End Sub
Private Sub RecordVote()
Dim strConnString As String = System.Configuration.Confi gurationMa nager.Conn ectionStri ngs.Item(" Conn").ToS tring()
Dim sqlConn As New SqlConnection(strConnStrin g)
sqlConn.Open()
Dim sqlCmd As New SqlCommand()
sqlCmd.CommandText = "IncrementVote"
sqlCmd.CommandType = Data.CommandType.StoredPro cedure
sqlCmd.Connection = sqlConn
' Creation parameters
Dim sqlParamQuestion As New SqlParameter("@PK_OptionId ", Data.SqlDbType.Int)
sqlParamQuestion.Value = rdoPollOptionList.Selected Value
sqlCmd.Parameters.Add(sqlP aramQuesti on)
' Execute stored procedure
sqlCmd.ExecuteNonQuery()
' Close connection
sqlConn.Close()
End Sub
stored procedure
(
@Pk_OptionId INT
)
AS
-- GETS THE CURRENT NUMBER OF VOTES FOR THAT OPTION
DECLARE @i_NumberOfVoteS INT
SELECT @i_NumberOfVotes = Votes
FROM PollOptions
WHERE PK_OptionId = @Pk_OptionId
UPDATE PollOptions
SET Votes = (@i_NumberOfVotes + 1)
WHERE PK_OptionId = @Pk_OptionId
RETURN
Protected Sub btnVote_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVote.Click
RecordVote()
'If Response.Cookies("Voted") Is Nothing Then
' Response.Cookies("Voted").
' Response.Cookies("Voted").
' lblError.Visible = False
' ' Checks if the user can still vote by using cookie
' RecordVote()
'Else
' lblError.Visible = True
'End If
'Response.Write(Response.C
'' Response.Redirect("test.as
End Sub
Private Sub RecordVote()
Dim strConnString As String = System.Configuration.Confi
Dim sqlConn As New SqlConnection(strConnStrin
sqlConn.Open()
Dim sqlCmd As New SqlCommand()
sqlCmd.CommandText = "IncrementVote"
sqlCmd.CommandType = Data.CommandType.StoredPro
sqlCmd.Connection = sqlConn
' Creation parameters
Dim sqlParamQuestion As New SqlParameter("@PK_OptionId
sqlParamQuestion.Value = rdoPollOptionList.Selected
sqlCmd.Parameters.Add(sqlP
' Execute stored procedure
sqlCmd.ExecuteNonQuery()
' Close connection
sqlConn.Close()
End Sub
ASKER
I excuted the stored procedure that works great.
I used
If Page.IsPostBack Then
' RecordVote()
Dim strConnString As String = System.Configuration.Confi gurationMa nager.Conn ectionStri ngs.Item(" Conn").ToS tring()
Dim sqlConn As New SqlConnection(strConnStrin g)
sqlConn.Open()
Dim sqlCmd As New SqlCommand()
sqlCmd.CommandText = "IncrementVote"
sqlCmd.CommandType = Data.CommandType.StoredPro cedure
sqlCmd.Connection = sqlConn
' Creation parameters
Dim sqlParamQuestion As New SqlParameter("@PK_OptionId ", Data.SqlDbType.Int)
sqlParamQuestion.Value = rdoPollOptionList.Selected Value
sqlCmd.Parameters.Add(sqlP aramQuesti on)
' Execute stored procedure
sqlCmd.ExecuteNonQuery()
' Close connection
sqlConn.Close()
instead of not Page.ispostback because the other way did not execute.
But it only finds Value 1 not value 2, 3 ect.
I used
If Page.IsPostBack Then
' RecordVote()
Dim strConnString As String = System.Configuration.Confi
Dim sqlConn As New SqlConnection(strConnStrin
sqlConn.Open()
Dim sqlCmd As New SqlCommand()
sqlCmd.CommandText = "IncrementVote"
sqlCmd.CommandType = Data.CommandType.StoredPro
sqlCmd.Connection = sqlConn
' Creation parameters
Dim sqlParamQuestion As New SqlParameter("@PK_OptionId
sqlParamQuestion.Value = rdoPollOptionList.Selected
sqlCmd.Parameters.Add(sqlP
' Execute stored procedure
sqlCmd.ExecuteNonQuery()
' Close connection
sqlConn.Close()
instead of not Page.ispostback because the other way did not execute.
But it only finds Value 1 not value 2, 3 ect.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Private Sub DisplayPoll()
Try
Dim ds As DataSet = getActivePoll()
' Displays the poll
lblPollQuestion.Text = ds.Tables(0).Rows(0)("Ques tion")
Dim i As Integer = 0
For Each dr As DataRow In ds.Tables(1).Rows
rdoPollOptionList.Items.Ad d(dr("Answ er"))
rdoPollOptionList.Items(i) .Value = dr("PK_OptionId")
rdoPollOptionList.Selected Index = 0
i = i + 1
Next
Catch ex As Exception
Throw ex
End Try
End Sub
Try
Dim ds As DataSet = getActivePoll()
' Displays the poll
lblPollQuestion.Text = ds.Tables(0).Rows(0)("Ques
Dim i As Integer = 0
For Each dr As DataRow In ds.Tables(1).Rows
rdoPollOptionList.Items.Ad
rdoPollOptionList.Items(i)
rdoPollOptionList.Selected
i = i + 1
Next
Catch ex As Exception
Throw ex
End Try
End Sub
ASKER
The radio button is getting the option value from the database.
where are you calling this DisplayPoll() ?
Put the Call to this method in If Not Page.IsPostBack
Put the Call to this method in If Not Page.IsPostBack
ASKER
Anythingin your Page_Load method? found it thanks
1: Execute your StoredProc directly from SqlServer and see if it is working as desired.
2: set breakpoint at line:
------> sqlParamQuestion.Value = rdoPollOptionList.Selected
Confirm the "rdoPollOptionList.Selecte
Post your observations.
Now a question: How is your rdoPollOptionList DataBound? i.e. dynamically in code-behind?
If so post that code....make sure that you DataBind it once only i.e. Not Page.IsPostBack.