Hi,

How can i get this on screen ?

sql= new sqlcommand("select count(*) from datas", database123)
...
...
persons=sql.executereader()
...
...

label1.text= ????

Thanks

Dani AI

Generated

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.

Recommended Answers

All 2 Replies

like this

sql= new sqlcommand("select count(*) as Count from datas", database123)
...
...
persons=sql.executereader()
...
...

label1.text= persons["Count"] ;

Thank you campkev, you are my best friend now :mrgreen:

Tahnks very very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.