验证提交数据

时间:2017-03-19 15:46:21

标签: c# visual-studio

我是编程和C#的新手,我试图建立一个小型电子投票系统,当选民投票选出过多的候选人时,如何使投票无效。例如:用户在议员职位上投票选出了7名候选人,而不是6名投票人,我怎样才能使他的投票无效或者不让他提交投票,直到他投票为止。

private void btn_submit_Click(object sender, EventArgs e)
    {
        con.Open();
        if (MessageBox.Show("Confirm and View your Votes?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            MessageBox.Show("Voting Successful", "Application Closed!", MessageBoxButtons.OK);

            using (SqlCommand command = new SqlCommand("UPDATE candidate SET cTally = cTally + 1 where cName like @cname or cName like @vName", con))
            {

                command.Parameters.AddWithValue("@cname", cb_president.Text);
                command.Parameters.AddWithValue("@vName", cb_vpresident.Text);
                command.ExecuteNonQuery();

            }
            foreach (object item in lb_councilor.SelectedItems)
            {
                using (SqlCommand command = new SqlCommand("UPDATE candidate SET cTally = cTally + 1 where cName like @coname", con))
                {

                    command.Parameters.AddWithValue("@coname", (item as DataRowView)["cName"].ToString());

                    Console.WriteLine((item as DataRowView)["cName"].ToString());
                    command.ExecuteNonQuery();

                }
                using (SqlCommand command = new SqlCommand("UPDATE voters SET isVoted = 1 where userName like @uname", con))
                {

                    command.Parameters.AddWithValue("@uname", userid );

                    command.ExecuteNonQuery();

                }
            }
            this.Close();
            new Form4().Show();
            this.Hide();


        }
        else
        {
            this.Activate();
        }   
    }

    private void Frm_voteview_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(Properties.Settings.Default.VotingSystemv2ConnectionString);
        con.Open();

        // TODO: This line of code loads data into the 'votingSystemv2DataSet7.candidate' table. You can move, or remove it, as needed.
        this.candidateTableAdapter2.Fill(this.votingSystemv2DataSet7.candidate);
        // TODO: This line of code loads data into the 'votingSystemv2DataSet5.candidate' table. You can move, or remove it, as needed.
        this.candidateTableAdapter1.Fill(this.votingSystemv2DataSet5.candidate);
        // TODO: This line of code loads data into the 'votingSystemv2DataSet4.candidate' table. You can move, or remove it, as needed.
        this.candidateTableAdapter.Fill(this.votingSystemv2DataSet4.candidate);

    }

    private void dgv_councilor_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {

    }

    private void dgv_councilor_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {

    }

    private void Frm_voteview_FormClosed(object sender, FormClosedEventArgs e)
    {
        SqlConnection con = new SqlConnection(Properties.Settings.Default.VotingSystemv2ConnectionString);
        // TODO: This line of code loads data into the 'votingSystemv2DataSet7.candidate' table. You can move, or remove it, as needed.
        this.candidateTableAdapter2.Fill(this.votingSystemv2DataSet7.candidate);
        // TODO: This line of code loads data into the 'votingSystemv2DataSet5.candidate' table. You can move, or remove it, as needed.
        this.candidateTableAdapter1.Fill(this.votingSystemv2DataSet5.candidate);
        // TODO: This line of code loads data into the 'votingSystemv2DataSet4.candidate' table. You can move, or remove it, as needed.
        this.candidateTableAdapter.Fill(this.votingSystemv2DataSet4.candidate);
        con.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        new Form9().Show();
    }

    private void cb_president_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void lb_councilor_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

}

1 个答案:

答案 0 :(得分:0)

目前,您的代码正在直接写入数据库,并从数据库中提取您的视图。绘制这种关系的图表如下:

View < > Database 

如果您尝试使用Separation of Concerns,则可能会更容易解决此问题。

使用C#,我们可以创建对象来跟踪我们在View和数据库之间的一些逻辑。这看起来更像是:

View < > Poll < > Database 

更详细地说,您的对象可能如下所示:

using System.Collections.Generic;

public class Poll
{
    var this.Candidates = new List<Candidate>();
    var this.MaxVotes;

    Poll(List<Candidate> candidates, int allowedVotes)
    {
       this.Candidates = candidates;
       this.MaxVotes = allowedVotes;
    }

    public Vote(List<Candidate> selectedCandidates)
    {
       if (selectedCandidates.Count > this.MaxVotes)
       {
           // throw a new Exception or warn the user if the user has too many votes and make them fix it
       }

       else // If the vote passes our check, we can save it to the database
       {
          // save the votes to the database
       }
    }
}

public class Candidate
{
    this.Name;

    Candidate(string name)
    {
       this.Name = name;
    }
}