473,503 Members | 1,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Retrieve COUNT from SQL Query - What am I doing wrong?

Here is what I have:

private int NationalCount()
{
Int32 numRecords = 0;
using (SqlConnection dataConnection = new
SqlConnection(GlobalVars.sqlConnString))
{
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText = ("SELECT Count(T1.ReportDate) FROM
from ScoutReportsNFS T1 WHERE (guidPlayerPersonID = '" + strCurrentPlayerID +
"') RETURN COUNT(*)");
try
{
dataConnection.Open();
dataCommand.Connection = dataConnection;
numRecords = (Int32)dataCommand.ExecuteScalar();
}
catch (Exception errLog) {
MessageBox.Show(Convert.ToString(errLog)); }
finally { if (dataConnection.State == ConnectionState.Open)
{ dataConnection.Close(); } }
}
return numRecords;
}

It just gives me an error. I believe all my problems are related
specifically to the query. What the heck am I doing wrong??? Thanks guys...
Jul 14 '08 #1
4 6181

"Todd Jaspers" <To*********@discussions.microsoft.comwrote in message
news:CA**********************************@microsof t.com...
Here is what I have:

private int NationalCount()
{
Int32 numRecords = 0;
using (SqlConnection dataConnection = new
SqlConnection(GlobalVars.sqlConnString))
{
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText = ("SELECT Count(T1.ReportDate)
FROM
from ScoutReportsNFS T1 WHERE (guidPlayerPersonID = '" +
strCurrentPlayerID +
"') RETURN COUNT(*)");
try
{
dataConnection.Open();
dataCommand.Connection = dataConnection;
numRecords = (Int32)dataCommand.ExecuteScalar();
}
catch (Exception errLog) {
MessageBox.Show(Convert.ToString(errLog)); }
finally { if (dataConnection.State == ConnectionState.Open)
{ dataConnection.Close(); } }
}
return numRecords;
}

It just gives me an error. I believe all my problems are related
specifically to the query. What the heck am I doing wrong??? Thanks
guys...

Your query is a little strange. You only need to do "Select Count(...) From
....". You don't have to add "Return Count(*)".
Jul 14 '08 #2
Ok, I figured it out:
Int32 numRecords = 0;
using (SqlConnection dataConnection = new
SqlConnection(GlobalVars.sqlConnString))
{
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText = ("SELECT Count(T1.ReportDate) as
numRecords FROM ScoutReportsNFS T1 WHERE (guidPlayerPersonID = '" +
strCurrentPlayerID + "')");
try
{
dataConnection.Open();
dataCommand.Connection = dataConnection;
SqlDataReader dataReader = dataCommand.ExecuteReader();
while (dataReader.Read())
{
if (dataReader["numRecords"].ToString() != null) {
numRecords = Convert.ToInt32((dataReader["numRecords"].ToString())); }
}
//numRecords = (Int32)dataCommand.ExecuteScalar();
}
catch (Exception errLog) {
MessageBox.Show(Convert.ToString(errLog)); }
finally { if (dataConnection.State == ConnectionState.Open)
{ dataConnection.Close(); } }
}
return numRecords;

Jul 14 '08 #3
Todd Jaspers wrote:
Here is what I have:

private int NationalCount()
{
Int32 numRecords = 0;
using (SqlConnection dataConnection = new
SqlConnection(GlobalVars.sqlConnString))
{
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText = ("SELECT Count(T1.ReportDate) FROM
from ScoutReportsNFS T1 WHERE (guidPlayerPersonID = '" + strCurrentPlayerID +
"') RETURN COUNT(*)");
try
{
dataConnection.Open();
dataCommand.Connection = dataConnection;
numRecords = (Int32)dataCommand.ExecuteScalar();
}
catch (Exception errLog) {
MessageBox.Show(Convert.ToString(errLog)); }
finally { if (dataConnection.State == ConnectionState.Open)
{ dataConnection.Close(); } }
}
return numRecords;
}

It just gives me an error. I believe all my problems are related
specifically to the query. What the heck am I doing wrong??? Thanks guys...
Leave out "RETURN COUNT(*)". RETURN statements are only supported from
stored procedures and user-defined functions. .ExecuteScalar() will take the
first column of the first row of the first result set of your query and
return that as the result, so no further action on your part is necessary.
Your query also contains a syntax error in the form of a duplicate "from".

Also, you do not need the "finally" block, as closing the connection will be
taken care of when the using block exits. You *should* wrap the SqlCommand
in a using, though, and you *should* use strongly-typed parameters, not
textual substitution, to pass values. So just make it

private int NationalCount() {
using (SqlConnection dataConnection = new
SqlConnection(GlobalVars.sqlConnString)) {
dataConnection.Open();
using (SqlCommand dataCommand = dataConnection.CreateCommand()) {
dataCommand.CommandText = "SELECT Count(T1.ReportDate) FROM
ScoutReportsNFS T1 WHERE guidPlayerPersonID = @playerID";
dataCommand.Parameters.AddWithValue("@playerID", new
Guid(strCurrentPlayerID));
return (int) dataCommand.ExecuteScalar();
}
}
}

There's no point to catching an exception here to show in a message box;
this isn't dealing with the problem. If you need this, put it in a function
on a higher level.

--
J.
Jul 14 '08 #4
Todd Jaspers wrote:
Here is what I have:

private int NationalCount()
{
Int32 numRecords = 0;
using (SqlConnection dataConnection = new
SqlConnection(GlobalVars.sqlConnString))
{
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText = ("SELECT Count(T1.ReportDate) FROM
from ScoutReportsNFS T1 WHERE (guidPlayerPersonID = '" + strCurrentPlayerID +
"') RETURN COUNT(*)");
try
{
dataConnection.Open();
dataCommand.Connection = dataConnection;
numRecords = (Int32)dataCommand.ExecuteScalar();
}
catch (Exception errLog) {
MessageBox.Show(Convert.ToString(errLog)); }
finally { if (dataConnection.State == ConnectionState.Open)
{ dataConnection.Close(); } }
}
return numRecords;
}

It just gives me an error. I believe all my problems are related
specifically to the query. What the heck am I doing wrong??? Thanks guys...
In addition to what has been said here, I'd like to add that my
strCurrentPlayerID is "foo');DROP TABLE ScoutReportsNFS;--"

Alun Harford
Jul 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3082
by: Paxton | last post by:
Hi, I'm trying to display the total page views per page within a given date range, but the correct SQL is seemingly beyond me. I get the correct result with a straightforward Group By and Count...
3
9271
by: thomasp | last post by:
I am trying to get a record count of a PHP query on a MS Acess database using ODBC with a DSN for MS ACCESS connection. I got this code from the PHP manual user notes. It seems to return the...
6
11480
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number of true records I want to use the result of that...
4
1684
by: m_houllier | last post by:
STUDENT TABLE StudentReference Student Name etc ATTENDANCE TABLE AttendanceID CourseID StudentReference
4
5635
by: max | last post by:
I am beginning to learn sql and need some help. I have a table of customers with their addresses. Let's say I want to run a query returning the number of customers whose last name is "Smith" by...
13
3399
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end...
4
3156
by: Simon Gare | last post by:
Hi all, I am trying to retrieve a count of booking entries made 30 days ago, below is the end of the query I am having problems with. dbo.booking_form.TimeOfBooking = DATEADD(day, -30,...
5
2488
by: Genalube | last post by:
I am trying to count the number of owners that show up in a query (conveyQuery). The query will produce a column OwnName that will contain names like John Smith, Mike Jones, Frank Vaugn. Each of...
2
1656
by: John | last post by:
I am having trouble getting this code to work, and was wondering if someone could tell me what I am doing wrong. -------------------------------------- CODE--------------------------------------...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.