Hi everyone,
I hava a code to update sql table, but when i look at the table nothings happen its empty this is my code:
Vote = count.ToString();
conn.Open();
CountUpdate.Parameters.Add(SqlCommand CountUpdate = new SqlCommand("UPDATE FinalVotes SET Name='"+btnCandidates1.Text+"' where Votes='"+Vote+"'", conn);
CountUpdate.Parameters.Add("@Name", SqlDbType.VarChar,100).Value=DBNull.Value;"@Votes", SqlDbType.VarChar,100).Value =DBNull .Value;
CountUpdate.ExecuteNonQuery();
conn.Close();
please help me out this problem...thanks
Loading


VulpesRank #55 · 29.2K reputationPosted Sep 5, 2011, 11:47 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Sep 5, 2011, 11:08 AM EDT
You don't show what CountUpdate is. I assume it is a SqlCommand, and if so then you need to set the CommandText Property and you are not doing that.
Sam HobbsRank #107 · 20.5K reputationPosted Sep 5, 2011, 12:43 PM EDT
Black Diamond191 reputationPosted Sep 5, 2011, 4:22 PM EDT
{
if (CaptainVoteCheckbox1.Visible == false)
{
CaptainVoteCheckbox1.Visible = true;
int countvote=0;
for (int i = 0; i <= countvote; i++)
{
MessageBox.Show("You have Only One Captain to Vote!");
btnCandidates2.Enabled = false;
btnCandidates3.Enabled = false;
btnCandidates4.Enabled = false;
btnCandidates5.Enabled = false;
try
{
string insert = "INSERT INTO CaptainVotes (Name) VALUES (@Param)";
SqlCommand cmd = new SqlCommand(insert, conn);
cmd.Parameters.Add("@Param", SqlDbType.VarChar, 100);
cmd.Parameters["@Param"].Value = btnCandidates1.Text;
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
string Vote;
int count = 0;
SqlCommand cmd1 = new SqlCommand("Select * from CaptainVotes where Name='" + btnCandidates1.Text + "'", conn);
SqlDataReader dr = null;
conn.Open();
dr = cmd1.ExecuteReader();
while (dr.Read() == true)
{
++count;
}
conn.Close();
Vote = count.ToString();
conn.Open();
SqlCommand CountUpdate = new SqlCommand("UPDATE FinalVotes SET Name = @Name where Votes = @Votes", conn);
CountUpdate.Parameters.Add("@Name", SqlDbType.VarChar,100).Value = btnCandidates1.Text;
CountUpdate.Parameters.Add("@Votes", SqlDbType.VarChar, 100).Value = Vote;
CountUpdate.ExecuteNonQuery();
conn.Close();
}
Hi guy's,
This is my complete code in my button where i place this code ....because still nothing happen in my sql ...even if i replace my some of the code you'de advice....thanks for the continuing support...
Satish BhatRank #2,496 · 563 reputationPosted Sep 5, 2011, 10:54 PM EDT
Also I didn't understand the following statements:
.
.
In the above code, the for loop will run only once. Or did you miss any line which updates countvote? Otherwise you can remove the for loop.