Reputation:
I m trying to count the rows where SomeColumn=SomeValue
. My code is shown below. It shows nothing.
using (SqlConnection conn = new SqlConnection("ConnectionString"))
{
conn.Open();
string selectStatement = "Select count(*) from Jobs where 'JobCategory=cse'";
SqlCommand cmd = new SqlCommand(selectStatement, conn);
int count = (int)cmd.ExecuteScalar();
Label1.Text = count.ToString();
conn.Close();
What step should I take?
Upvotes: 0
Views: 1150
Reputation: 133
Please use this
string selectStatement = "Select COUNT(*) as cnt from [Jobs] where JobCategory='cse'";
Upvotes: 0
Reputation: 98740
Your single quotes are in wrong place. Try like this;
string selectStatement = "Select count(*) from Jobs where JobCategory = 'cse'";
And please parameterize your sql query always. This could be reason on SQL Injection attack. Here how you can use it;
string realcse = "Write here which value you want to filter in where clause";
string selectStatement = "Select count(*) from Jobs where JobCategory = @cse";
SqlCommand cmd = new SqlCommand(selectStatement, conn);
cmd.Parameters.AddWithValue("@cse", realcse);
In your case, it should be like;
string realcse = "cse";
cmd.Parameters.AddWithValue("@cse", realcse);
Upvotes: 0
Reputation: 6079
Change 'JobCategory=cse' to JobCategory = 'cse'
Like
string selectStatement = "Select count(*) from Jobs where JobCategory = 'cse'";
OR try like this
string selectStatement = "Select count(*) from Jobs where JobCategory Like 'cse'";
Check the query once in SQL before implementing it in code. Whether your getting the expected result or not check.
Upvotes: 1
Reputation: 8630
string selectStatement = "Select COUNT(*) from [Jobs] where JobCategory='cse'";
Upvotes: 0