views:

1658

answers:

3

Hi All,

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...

+2  A: 

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();
}
John Millikin
Thanks John, I will give it a try
Jason
+1  A: 

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).

Matt Hamilton
So I should change it to:public int CountCategory(int intCategoryID){}Also How would change it to not be a return type of SqlDataReader?
Jason
Check out @John Millikin's code above. If you want to return an int from CountCategory, then just use "return CategoryBLL.GetMainCatCount(intCategoryID);".
Matt Hamilton
A: 
 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);

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