I am trying to count the number of rows and pass that count value to do some logic. Below is the code:
static public int GetNoImagesofMakeID(int makeID)
{
string sql = "Select COUNT(*) from makeImages";
SqlDataAdapter da = new SqlDataAdapter(sql, ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
return Convert.ToInt32(dt.Rows.Count);
}
protected void hideUnhideFileUpload(int makeID)
{
int count = caravans.GetNoImagesofMakeID(makeID);
if (count >= 10)
{
FileUpload2.Enabled = false;
}
else
{
FileUpload2.Enabled = true;
}
}
I don’t know for what reason the count is always 1. I checked the same query on SQL Server and it’s working fine. But, here dt.rows.count always return 1.
Any help will be highly appreciated. Thanks.
Your query is returning one row with one value (the count of rows), so
dt.Rows.Countis correctly returning 1 (meaning 1 row returned by your query).Using a datatable, you would find the count returned by your query at
dt.Rows[0][0](row 0 column 0 of your datatable)