How do I get a the count back from this query.

bmanmike39
bmanmike39 used Ask the Experts™
on
I would like to get the number of rows that have the file name 1234567-1.jpg  from the table field imgName  but it returns 0 every time.  I cant figure out what is wrong with my code.

Code:

SqlConnection sqlConnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            Object returnValue;

           

           cmd.CommandText = "SELECT * FROM wsPageImg WHERE imgName = '1234567-1.jpg'";

            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();

          

            SqlDataReader reader = cmd.ExecuteReader();



            if (reader.Read())
            {
                Label9.Text = reader[0].ToString();
            }



            sqlConnection1.Close();

            
        }
        
        int b = int.Parse(Label9.Text);

     
        if (b == 0)
{
// action
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"SELECT Count(*) FROM wsPageImg WHERE imgName = '1234567-1.jpg'";

Commented:
well, if you want the query to return the number of rows where that string is stored into that field, change your query to :

SELECT count(*) FROM wsPageImg WHERE imgName = '1234567-1.jpg'

Commented:
ooops, beat to the punch...sorry
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Author

Commented:
This returns a ) value every time for me.  i  see the info in the bd table.

Author

Commented:
I made the COUNT(*) change but get the same results
do you have rows that meet that queries criteria?>

Commented:
can you put a stop on this line:

Label9.Text = reader[0].ToString();

and check the reader value in your immediate window?

it should be simply a number.  you could switch over to a query window too and run the query to make sure it returns a vallid number.

Commented:
even if there are no results, the selection of count(*) will return a zero.
"SELECT Count(*) as Count FROM wsPageImg WHERE imgName = '1234567-1.jpg'";

  if (reader.Read())
            {
                Label9.Text = reader["Count"].ToString();
            }

Author

Commented:
yes,  I know that one of the rows has the value 1234567-1.jpg

but i alway get 0

Author

Commented:
Didn’t work  p davis
&
ViaTom It shows 0
"SELECT Count(*) as Count FROM wsPageImg WHERE imgName LIKE '%1234567-1.jpg%'";

  if (reader.Read())
            {
                Label9.Text = reader["Count"].ToString();
            }


maybe you have traiing whitespace -- i don't know if your db allows wild cards or not.

Author

Commented:
It was white space,  thanks!!!!!!!!!!!!

Author

Commented:
Thank you,  that was it!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial