3

I am making a voting system. I have a problem adding a count(increment each time the candidate is voted) to each candidate that they voted.

My Vote Button code is :

    sc.Open();
        try
        {
            cmd = new SqlCommand("UPDATE TableVote SET Vcount=Vcount+1 WHERE id=@count", sc);

            cmd.Parameters.AddWithValue("@count", TxtPresID.Text);

            int res = cmd.ExecuteNonQuery();
            if (res > 0)
            {
                MessageBox.Show(TxtPresID.Text.ToString() + " Saved!");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }

and my table is

my sql

the result of Vcount is just NULL..

null

What am i doing wrong? or is my Sql statement correct?

4
  • try Vcount=ISNULL(Vcount,0)+1 Commented Oct 28, 2013 at 16:00
  • 2
    Do not allow Null to Vcount Commented Oct 28, 2013 at 16:00
  • 2
    Don't allow NULL at all. Something that doesn't have any votes against it would have a vote count of 0, not null! Commented Oct 28, 2013 at 16:00
  • What "result" do you mean - in the table, or are you trying to pull the new vote count back to the user? Commented Oct 28, 2013 at 16:00

7 Answers 7

4

Looks like your Vcount is NULL. As others have pointed out, you should make that column default to 0 or you could do this

UPDATE TableVote SET Vcount=ISNULL(Vcount, 0) + 1 WHERE id=@count
Sign up to request clarification or add additional context in comments.

Comments

3

Set the default Value of Vcount to be 0. It shouldn't be a null column.

Here is the query

ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}

ALTER TABLE TableVote 
ADD Vcount int NOT NULL 
CONSTRAINT  tablevote_vcount_default DEFAULT 0

Another option is to check, whether Vcount is NULL

UPDATE TableVote SET Vcount=ISNULL(Vcount, 0) + 1 WHERE id=@count

1 Comment

Column names in each table must be unique. Column name 'Vcount' in table 'TableVote' is specified more than once.
3

You shouldn't be updating the count row every time. if you have 2 votes incoming at the same time, it might have unpredictable consequences.

A better way would be to have a table "votes" and then insert a row for every vote, including the candidate ID, the voter ID and the date of vote. also means you can have historical data for different voters, and that you can easily change votes if needed.

2 Comments

This is a very good point, so I'll give it a +1, but it does not answer the question.
It does not answer the question, but I think it is a very valid point. Nice catch. +1
2

Learn that

Null + 1 = Null

So your Vcount will always be null when adding 1.
Vcount must be not nullable, and it's default value muste be 0. Then when you will add 1 to 0, it will give you the good result.

Comments

1

You can also Change your SQL Statement so that zero is used if the VCOUNT-Column is null:

"UPDATE TableVote SET Vcount=ISNULL(Vcount, 0)+1 WHERE id=@count"

This is especially useful if you cannot change the database schema to be Not-Null and have a Default value.

Comments

1

Vcount must be have a default 0 value, and Not Null, cause is an Int Field.

Comments

1

If i am not wrong why not just use your primary key for it ,

UPDATE TableVote SET Vcount= max(id)  WHERE id=@count

OR

If some id's are deleted and you don't want to keep records or count deleted votes then

  UPDATE TableVote SET Vcount= COUNT(id)  WHERE id=@count

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.