Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 提交数据的验证_C#_Visual Studio - Fatal编程技术网

C# 提交数据的验证

C# 提交数据的验证,c#,visual-studio,C#,Visual Studio,我对编程和C#还不熟悉,我正在尝试制作一个小型电子投票系统,当选民投票过多的候选人时,我如何使投票无效。例如:用户投票给7名候选人,而不是6名议员,我如何才能使他的投票无效或在他投票到6名之前不让他提交投票 private void btn_submit_Click(object sender, EventArgs e) { con.Open(); if (MessageBox.Show("Confirm and View your Votes?", "C

我对编程和C#还不熟悉,我正在尝试制作一个小型电子投票系统,当选民投票过多的候选人时,我如何使投票无效。例如:用户投票给7名候选人,而不是6名议员,我如何才能使他的投票无效或在他投票到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)
    {

    }
}

}

目前,您的代码正在直接写入数据库,并从数据库进入视图。绘制该关系的图表如下所示:

View < > 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;
    }
}
查看<>数据库
如果你尝试一个简单的方法,你可能会发现解决这个问题更容易

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

View < > Poll < > Database 
查看<>投票<>数据库
更详细地说,您的对象可能看起来像:

View < > 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;
    }
}
使用System.Collections.Generic;
公众阶级投票
{
var this.Candidates=新列表();
var this.maxvoces;
投票(列出候选人,不允许投票)
{
这个。候选者=候选者;
this.maxvoces=允许投票;
}
公众投票(列出选定的候选人)
{
if(selectedCandidates.Count>this.maxvoces)
{
//如果用户有太多的投票权,则引发新异常或警告用户,并让他们修复该异常
}
否则//如果投票通过我们的检查,我们可以将其保存到数据库中
{
//将投票保存到数据库中
}
}
}
公开课候选人
{
这个名字;
候选(字符串名称)
{
this.Name=Name;
}
}

当前,您的代码正在直接写入数据库,并从数据库进入视图。绘制该关系的图表如下所示:

View < > 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;
    }
}
查看<>数据库
如果你尝试一个简单的方法,你可能会发现解决这个问题更容易

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

View < > Poll < > Database 
查看<>投票<>数据库
更详细地说,您的对象可能看起来像:

View < > 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;
    }
}
使用System.Collections.Generic;
公众阶级投票
{
var this.Candidates=新列表();
var this.maxvoces;
投票(列出候选人,不允许投票)
{
这个。候选者=候选者;
this.maxvoces=允许投票;
}
公众投票(列出选定的候选人)
{
if(selectedCandidates.Count>this.maxvoces)
{
//如果用户有太多的投票权,则引发新异常或警告用户,并让他们修复该异常
}
否则//如果投票通过我们的检查,我们可以将其保存到数据库中
{
//将投票保存到数据库中
}
}
}
公开课候选人
{
这个名字;
候选(字符串名称)
{
this.Name=Name;
}
}

您从哪个控件获得选票?您指的是我从哪个控件获得选票@EmrahSüngüI无法完全阅读您的代码,但我不知道用户投票给谁。编辑:哦,你正在直接更新表格,你从哪个控件获得选票?你说我从哪个控件获得选票是什么意思@EmrahSüngüI无法完全阅读您的代码,但我不知道用户投票给谁。编辑:哦,您正在直接更新表