Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count Function

Author  Topic 

ML101
Starting Member

8 Posts

Posted - 2011-04-26 : 05:59:32
Hi I'm new to SQL and I was wondering if using Count Function I need to also make a Table to see the results or I it makes it's own table.
(I'm not talking about an DataBase Table)

Thanks

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-26 : 06:01:09
Select SomeCol, SomeCount = count(*) From someTable Group By SomeCol

This will return 2 columns of data, SomeCol and SomeCount

Corey

I Has Returned!!
Go to Top of Page

ML101
Starting Member

8 Posts

Posted - 2011-04-26 : 06:11:06
Thank you :)
Go to Top of Page

ML101
Starting Member

8 Posts

Posted - 2011-04-26 : 06:18:59
Sorry But I have another question..
So if I don't need to build the table how do I write it?
These is what I wrote:
<script runat="server">
string st = " ";

public void Page_load()
{
if (Session["Admin"].Equals(false))
{
Response.Redirect("NoEntryManger.htm");
}
else
{
string stcon = " Data Source=.\\SQLEXPRESS;AttachDbFilename=C:///\\App_Data\\Database3.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(stcon);
string STsql = "select*Count(Status) From TableUser WHERE Status='Old'";
SqlDataAdapter datadapter = new SqlDataAdapter(STsql, conn);
DataSet ds = new DataSet();
datadapter.Fill(ds);
DataTable dt = ds.Tables[0];
StringBuilder htmlstr = new StringBuilder();
htmlstr.Append("<table border=5>");
htmlstr.Append("<table border=5>");
htmlstr.Append("<tr>");
htmlstr.Append("<td>");
htmlstr.Append("Old");
htmlstr.Append("</td>");
htmlstr.Append("</tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
htmlstr.Append("<tr>");
htmlstr.Append("<td>");
htmlstr.Append("<???>");
htmlstr.Append("</tr>");
}
htmlstr.Append("</table>");
st = htmlstr.ToString();
}

}

</script>
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-26 : 07:30:06
There are (at least) couple of things you need to change in your C# code.

First, the query needs to be what Corey gave earlier (or something similar. What you have currently will not be parsed correctly by the SQL server. So change it to:
string STsql = "select Count(Status) as StatusCount From TableUser WHERE Status='Old'";
That may or may not give you the results you are looking for, but at least this will compile and you will be able to look at the results.

Second, to get the count that is returned from the server do something like this:
htmlstr.Append("<td>");
htmlstr.Append(dt.Rows[i]["StatusCount"].ToString());
htmlstr.Append("</tr>");

Once you get it working, you may also want change the code to use ExecuteReader or ExecuteNonQuery instead of the DataAdapter.Fill method - because it both of those are faster and lightweight compared to using a data table and fill method.
Go to Top of Page

ML101
Starting Member

8 Posts

Posted - 2011-04-26 : 07:45:43
Thank You SO MUCH!!
Go to Top of Page
   

- Advertisement -