If I may add to the solution provided, I would strongly recommend you do the following:
(1) Always put app level configuration such as your connection string in your
web.config
file so you won't have to recompile and redeploy your code when you modify your connection string.
(2) Wrap objects that eat resources such as
SqlConenction
and
SqlCommand
within a
using block so objects will be automatically disposed after using them. Here's a quick example:
[WebMethod]
public void OnlineUsers_Counts()
{
int rowCount = 0;
using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){
using(SqlCommand cmd = new SqlCommand("SELECT count(*) FROM OnlineUsers" , connection)){
cmd.CommandType = CommandType.Text;
rowCount = cmd.ExecuteScalar();
}
Context.Response.Write(rowCount);
}