2017-03-20 18 views
-4

プログラミングとC#を初めて使用しています。電子投票システムを小型化しようとしています。投票者が投票数を超過した場合、投票を無効にするにはどうすればよいですか?例:ユーザーが評議員の位置に7つの候補の代わりに、6を投票し、どのように私は彼の投票は無効にすることができたり、彼がそれ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) 
    { 

    } 
} 

}

+4

サイド注:コードを掲示上の[MCVE]ガイダンスをお読みください。 –

答えて

1

作るまで、彼は彼の票を提出することはできません。 C#へようこそ。

わかりました。まず、あなたの質問に対する答え?コードのあなたの行を見てみましょう:

foreach (object item in lb_councilor.SelectedItems) 

...これは、選択されたアイテムのコレクションをループしている何をしていますか。 lb_councilor.SelectedItemsを入力してみてください.VisualStudioのインテリセンスが、IEnumerableなどのようなものを教えてくれることを願っています。

[これは、変数/どのようなintellisenseがそれをもたらすのかを知ることができます。]

IEnumerableにはCountメソッドがあります。

  • あなたがコードにその方法を見つけたすべてのオブジェクトに名前を付けることを確認してください:

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

    さて、それが邪魔だと、ここでいくつかの追加のものです:だからあなたの問題はほど簡単であるかもしれません。 candidateTableAdapter1、votingSystemv2DataSet5、label5、button1、Form9 - デフォルトの名前のアイテムを参照しているコードを手渡すなら、他の開発者の人生を悪夢にするでしょう。

  • 助けることができない限り、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); 
関連する問題