Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

Code is not calculating percentages correctly.

$
0
0
Greetings Experts,
 
For the past two days now, I have been struggling with this code.
 
Our users had just finished using our online app to cast ballots and I have been trying to accomplish three things at same time.
 
One, display each candidate's name, his/her total vote count, and percentage of the vote s/he received.
 
With the code below, I am able to successfully display candidate's name and the total vote count for each candidate.
 
However, I have not been able to figure out how to display the percentage of each candidate's total vote count.
 
In the code shown below, if I run the sql portion of the code on Sql Server Management Studio, it works great.
 
However, when integrated with my .net portion of the code, it keeps assigning every candidate the same percentage of 100%.
 
I would really appreciate your expertise on this.
 
Thanks a in advance for your assistance.
 

ProtectedSub DataList1_ItemDataBound(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem ThenDim questionid AsInteger
            questionid = CInt(CType(e.Item.FindControl("Label3"), Label).Text)
            Dim cnn AsNew SqlConnection(ConfigurationManager.ConnectionStrings("BallotsConnectionString").ConnectionString)
            Dim cmd AsNew SqlCommand("select CandidateId, CASE WHEN CurrentOfficeHolder='Incumbent' THEN CandidateName + ' ('+ CurrentOfficeHolder + ')' ELSE CandidateName END As CandidateName from Candidates where PositionId=@qid", cnn)
            Dim p1 AsNew SqlParameter("@qid", questionid)
            cmd.Parameters.Add(p1)
            Dim da AsNew SqlDataAdapter
            da.SelectCommand = cmd
            Dim ds AsNew DataSet
            da.Fill(ds, "choices")
 
            cnn.Open()
            ForEach row As DataRow In ds.Tables("choices").Rows
                Dim cmdAns AsNew SqlCommand("select DISTINCT "& _
                                             "count( IsNull(er.candidateId,0)) OVER (Partition by o.orgName, p.Position, c.candidateName,p.PositionId ) "& _
                                             "as VoteCount, "& _
                                             "CAST(count( IsNull(er.candidateId,0)) OVER (Partition by o.orgName, p.Position, c.candidateName,p.PositionId ) *1./count( IsNull(er.candidateId,0)) OVER (Partition by p.Position)*100 as decimal(6,2)) as Percentage "& _
                                             "from Organizations o inner join Positions p on o.OrgID = p.OrgID inner join Candidates c on p.positionId = c.positionId "& _
                                             "inner join ElectionResults er on c.candidateId = er.candidateId "& _
                                             "WHERE c.candidateid = @cid", cnn)
                Dim pCid AsNew SqlParameter("@cid", row("candidateid"))
                cmdAns.Parameters.Add(pCid)
 
                Dim dr = cmdAns.ExecuteReader()
                If dr.HasRows = TrueThenWhile dr.Read
                        Dim count AsInteger = dr(0)
                        Dim pct AsString = dr(1)
                        row("CandidateName") = row("CandidateName") & " - "& count & "("& pct & "%)"EndWhileEndIf
                dr.Close()
 
            Next
            cnn.Close()
            Dim list As BulletedList = e.Item.FindControl("BulletedList1")
            list.DataSource = ds
            list.DataTextField = "CandidateName"
            list.DataBind()
        EndIf 
    EndSub

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>