Hi,
How can i get this on screen ?
sql= new sqlcommand("select count(*) from datas", database123)
...
...
persons=sql.executereader()
...
...
label1.text= ????
Thanks
Hi,
How can i get this on screen ?
sql= new sqlcommand("select count(*) from datas", database123)
...
...
persons=sql.executereader()
...
...
label1.text= ????
Thanks
Quick tip: for a single numeric result (a row count) prefer a scalar call instead of a reader — it’s simpler and a bit faster. ’s reader-based approach will work, but a scalar call returns the first column of the first row directly, so you avoid reader.Read()/field lookups.
Example pattern (replace the comment with your actual COUNT query and your UI label):
using (var cmd = new SqlCommand("/* your COUNT query here */", conn))
{
object result = cmd.ExecuteScalar();
int total = (result == null || result == DBNull.Value) ? 0 : Convert.ToInt32(result);
myLabel.Text = total.ToString();
} If you stick with the reader approach shown earlier, remember to call Read() before accessing columns, check for DBNull.Value, and always close/dispose the reader (or use using). Also use parameterized SQL when the count is filtered by user input to avoid injection.
One more note: don’t run blocking DB calls on the UI thread — use async/await or a background worker for WinForms so the form stays responsive. For very large tables, COUNT can be expensive; if an exact number isn’t required, consider cached counts or filtered/indexed counts to improve performance.
Jump to Post— campkev 0like this
sql= new sqlcommand("select count(*) as Count from datas", database123)
...
...
persons=sql.executereader()
...
...label1.text= persons["Count"] ;
like this
sql= new sqlcommand("select count(*) as Count from datas", database123)
...
...
persons=sql.executereader()
...
...
label1.text= persons["Count"] ;
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.