hi everyone,
how to direct extract the value from database? I wan't to extract the votes value and convert it into integer, how can i do that?
SqlCommand decrementVotes=new SqlCommand("Select Votes FinalVotes where Names='"+btnCandidates1.Text+"'", conn);
conn.Open();
decrementVotes.ExecuteNonQuery();
conn.Close();
Loading

Satyapriya NayakPosted Sep 9, 2011, 8:06 AM
If you want to count the total votes and display it in a textbox
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
str = "select sum(votes)from vote_table";
com = new SqlCommand(str, con);
txt_totalvotes.Text = com.ExecuteScalar().ToString();
con.Close();
-----------------------------------------------------------------
If you want to know which person has voted how many times give the person name in txtname textbox output will be shown in txtvote textbox
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
str = "select votes from vote_table where name='" + txtname.Text + "'";
com = new SqlCommand(str, con);
txtvote.Text = com.ExecuteScalar().ToString();
con.Close();
Thanks
If this post helps you mark it as answer
Prabhu RajaPosted Sep 9, 2011, 6:26 AM
Try this
try
{
SqlCommand decrementVotes=new SqlCommand("Select Votes as FinalVotes where Names=@ButtonText");
conn.Open();
decrementVotes.Connection = con;
decrementVotes.Parameters["@ButtonText"].Value = btnText.Text;
int n = decrementVotes.ExecuteNonQuery();
if (n>0)
{
MessageBox.Show(n.toString()+" Votes");
}
else
{
MessageBox.Show("No Votes");
}
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Satish BhatPosted Sep 9, 2011, 6:20 AM
SqlCommand decrementVotes = new SqlCommand("Select Votes FinalVotes where Names='"+btnCandidates1.Text+"'", conn);
conn.Open();
int outputValue = (int)decrementVotes.ExecuteScalar();
conn.Close();
Please note your query should return SINGLE value. Here I am assuming that FinalVotes table contains single record (Votes) for the given candidate.