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

VulpesPosted Sep 5, 2011, 3:47 PM
Satish BhatPosted Sep 6, 2011, 2:54 AM
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.
Black DiamondPosted Sep 5, 2011, 8:22 PM
{
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...
Sam HobbsPosted Sep 5, 2011, 4:43 PM
Sam HobbsPosted Sep 5, 2011, 3:08 PM
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.