在提交之前验证数据

时间:2017-03-20 03:43:52

标签: c# visual-studio validation

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

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Data.SqlClient;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Frm_voteview : Form
{
    string userid;
    public Frm_voteview()
    {
        InitializeComponent();
        SqlConnection con = new SqlConnection(Properties.Settings.Default.VotingSystemv2ConnectionString);
        con.Open();
    }
    public Frm_voteview(string userid)
    {
        InitializeComponent();
        SqlConnection con = new SqlConnection(Properties.Settings.Default.VotingSystemv2ConnectionString);
        con.Open();
        this.userid = userid;
    }
    SqlConnection con = new SqlConnection(Properties.Settings.Default.VotingSystemv2ConnectionString);

    private void button3_Click(object sender, EventArgs e)
    {

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void label5_Click(object sender, EventArgs e)
    {

    }

    private void button7_Click(object sender, EventArgs e)
    {
       new Frm_Login().Show();
       this.Hide();
    }

    private void button8_Click(object sender, FormClosingEventArgs e)
    {

    }



    private void groupBox1_Enter(object sender, EventArgs e)
    {

    }




    private void Frm_voteview_FormClosing(object sender, FormClosingEventArgs e)
    {

    }

    private void Frm_voteview_Click(object sender, EventArgs e)
    {

    }


    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 :(得分:1)

欢迎来到C#。

好的,很少。首先,你的问题的答案?看看你的代码行:

foreach (object item in lb_councilor.SelectedItems)

......这样做是循环选择了一系列项目。好吧,尝试输入lb_councilor.SelectedItems - 希望你在VisualStudio中的intellisense会告诉你它是什么(可能是IEnumerable之类的东西。)

[这是VS的一个很好的技巧 - 输入变量/对象/什么,看看intellisense提出了什么。]

好吧,好消息是,像IEnumerable这样的东西有一个Count方法。所以你的问题可能很简单:

if (lb_councilor.SelectedItems.Count != 6)
{
    // uh oh.  Better stop them.

好的,现在已经不在了,还有一些额外的东西:

  • 确保为每个找到代码的对象命名。 candidateTableAdapter1,votingSystemv2DataSet5,label5,button1,Form9 - 如果你提交引用一堆默认命名项的代码,你将会让其他开发人员的生活成为一场噩梦。

  • 不要使用' object'除非你能帮助它,否则在环绕一个foreach时。这是您的代码正在做什么

    foreach (object something in somegroup)
    {
        use(something as DataRowView);
    }
    

...也就是说,你正在施展某些东西'作为一个对象......但之后你就将它用作DataRowView。你也可以这样做:

    foreach (DataRowView something in somegroup)

或者至少做这样的事情:

    foreach (object something in somegroup)
    {
        if (something is DataGridView)
        {
            // your code is safe from an unexpected data type now
            use(something as DataGridView);