I have following code where I get dataset through method readAvgReview. If dataset [0] table includes any avg values (between 1-10) I want to display values to the page.
If it doesn't include these values, I want to show label NoGrades (NoGrades Text="There is no reviews yet"):
protected void showReviews()
{
reviewDS = reviews.readAvgReview(); //this returns dataset with avg grades
if (reviewDS.Tables[0] != null)
{
NoGrades.Visible = false;
Label1_avg.Text = "Average: " + reviewDS.Tables["Averages"].Rows[0][0].ToString();
Label2_2.Text = "Overall: " + reviewDS.Tables["Averages"].Rows[0][1].ToString();
//couple more with similar pattern
}
else //This isn't working
{
NoGrades.Visible = true;
}
}
Sql -query behind is this:
SqlDataAdapter da = new SqlDataAdapter("Select round(avg(pointsAvg),1), round(avg(pointsOverall),1), round(avg(pointsArea),1), round(avg(pointsTidiness),1)" + "from reviews where id ='" + rewEn.id + "' ", c); //there is 2 queries, but this one is the problem
Query and returning dataset works great, because if there IS values in this table [0], they will be displayed.
Problem arises when there is not values in database meeting the sql criteria --> else part of the above code doesn't work.
I have tried to check if table has rows or not and what values there is in the first row first col. I've tried these different ways:
//Count if there is no rows at the table
reviewDS.Tables[0].Rows.Count() == 0;
//Count if there is no even table (I erased the other table from query when tried this)
reviewDS.Tables.Count() == 0;
//check value of the [0][0]:
reviewDS.Tables[0].Rows[0][0] == null;
reviewDS.Tables[0].Rows[0][0].ToString() == "";
reviewDS.Tables[0].Rows[0][0].ToString() == "0";
reviewDS.Tables[0].Rows[0][0].ToString() == " ";
I've also changed the order of if-else code parts without success.
So it looks like, that there always is dataset returned even if there is no real avg values (1-10)
And there is also always one row, because row count never returns true.
And that one row first column value is not "", " ", "0" or null.
So what am I missing here? The problem is not at the code behind because if there is values in database, it counts the avg:s and returns them correctly.