0

I am trying to use a SqlDataReader to count the amount of categories I use.

Here is my Business Logic Code:

// Return count of main categories for homepage
    [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
    public int GetMainCatCount(int intCategoryID)
    {
        intCategoryID = SQLInject(intCategoryID);

        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT COUNT(intCategoryID) "
                        + "FROM  tblCategory "
                        + "WHERE intCategoryID=" + intCategoryID;
        con.Open();
        return (Int32)cmd.ExecuteScalar();
    }

and here is my code in the code benhind page making the call:

public string CountCategory(int intCategoryID)
    {
        SqlDataReader myReader;
        myReader = CategoryBLL.GetMainCatCount(intCategoryID);

        myReader.Close();
        myReader.Dispose();
        return Convert.ToInt32(myReader);
    }

I want to use the results of the SqlDataReader to populate a label tag. When I try and run this code I recieve this error message:

Cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'

Could anyone please tell me where I am going wrong. Thanks...

3 Answers 3

2

Could anyone please tell me where I am going wrong. Thanks...

Sure: you're going wrong trying to convert an int to an SqlDataReader. You're also trying to convert an SqlDataReader to an int, later on.

Honestly, the types in your example are so screwed up that I can't even figure out what the code is supposed to do. To get started, read this.


Thinking more about it, here's a potential fixed version:

public string CountCategory(int intCategoryID)
{
    int count = CategoryBLL.GetMainCatCount(intCategoryID);
    return count.ToString();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your "GetMainCatCount" method returns an int, but you're assigning its return value to a variable of type SqlDataReader:

SqlDataReader myReader;
myReader = CategoryBLL.GetMainCatCount(intCategoryID);

Additionally, your CountCategory method is defined as returning a string, yet you're returning an Int32 from it (the result of a Convert.ToInt32 call on the last line).

2 Comments

So I should change it to: public int CountCategory(int intCategoryID) { } Also How would change it to not be a return type of SqlDataReader?
Check out @John Millikin's code above. If you want to return an int from CountCategory, then just use "return CategoryBLL.GetMainCatCount(intCategoryID);".
0
 myReader = CategoryBLL.GetMainCatCount(intCategoryID);

In that line, you're trying to convert an Int32 to an object of Type SqlDataReader.

You want to try myReader.GetMainCatCount(intCatagorID);

2 Comments

Other way around: he's trying to convert Int to SqlDataReader.
Whoops you're right. My post originally was commenting on " return Convert.ToInt32(myReader);" which converts SqlDataReader;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.