Role of ExecuteScalar ASP.NET
what is role of ExecuteScalar()?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Chetan koralli14 reputationPosted Jan 22, 2012, 7:14 PM EST
Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 22, 2012, 7:50 PM EST
Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored
Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database
Ex:-
Count the total votes and display it in a textbox
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string str = "select sum(votes)from vote_table";
SqlCommand com = new SqlCommand(str, con);
txt_totalvotes.Text = com.ExecuteScalar().ToString();
con.Close();
Thanks