-
Reader Error
I am getting a couple of errors in my page_load event
First Error:
Cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'
Second Error:
Operator '>=' cannot be applied to operands of type 'object' and 'int'
any ideas??? I am trying to return a count from the sql query, then check with the reader if that count is greater than 2.
Code:
private void Page_Load(object sender, System.EventArgs e)
{
string strConn = "Data Source=localhost;uid=steve;pwd=xxxxxx;Initial Catalog=mansion";
SqlConnection Conn = new SqlConnection(strConn);
SqlCommand Comm = new SqlCommand("SELECT rDate, COUNT(rDate) AS 'count' FROM reservations GROUP BY(rDate) HAVING COUNT(rDate) > 1", Conn);
SqlDataReader reader;
reader = Comm.ExecuteNonQuery();<------ First Error
while(reader.Read())
{
if(reader["count"] >= 2)<------------ Second Error
{
Calendar1.SelectedDates.Add(Convert.ToDateTime(reader["rDate"]));
}
}
reader.Close();
Conn.Close();
Conn = null;
}
-
I got it figured out.
First Error: ExecuteNonQuery() is used for Insert, update and delete, not when a query will return rows...So I changed it to ExecuteReader();
Second Error: The reader returns an object type field, so to get the integer value in "count" you have to convert it to an integer
Convert.ToInt32(reader["count"]);
and that fixed the problem!