1

Possible Duplicate Here

Does anybody know a good way of finding out if a SqlDataSource will return Zero Rows?

This is my call to the SQL Database in my C# code behind:

        string SQL_PROD_DOCS =
            @"SELECT   TOP 3  Documents.ProductID, Documents.ProjectID, Documents.DocTitle, Documents.DocURL
              FROM         Products INNER JOIN
                  Documents ON Products.ID = Documents.ProductID
              WHERE     (Products.ShowOnScorecard = 1) AND (Products.Deleted = 0) AND (Products.ID = 15) AND (Documents.ProjectID IS NULL) AND (Documents.Deleted = 0) AND (Documents.AttributeID = 1)";


        SqlDataSource dsProdDocsHeader = new SqlDataSource(utils.Conn(Server.MachineName), SQL_PROD_DOCS);
        RptrProductDocs.DataSource = dsProdDocsHeader.Select(DataSourceSelectArguments.Empty);
        RptrProductDocs.DataBind();

I want to find if dsProdDocsHeader will return zero so I can return a different message instead.
Thank you!

1
  • 1
    I'd build an object from the datareader, check that for a 0 Count and then Bind it. Commented Aug 9, 2013 at 17:33

3 Answers 3

2

Try This.

    SqlDataSource dsProdDocsHeader = new SqlDataSource(utils.Conn(Server.MachineName), SQL_PROD_DOCS);
    DataView dvsqllist = (DataView)dsProdDocsHeader.Select(DataSourceSelectArguments.Empty);
    DataTable tablelist = dvsqllist.Table;
    int n = tablelist.Rows.Count;
    if (n > 0)
    {
       RptrProductDocs.DataSource = dsProdDocsHeader.Select(DataSourceSelectArguments.Empty);
       RptrProductDocs.DataBind();
    }
    else
    {
       // whatever u want to show.
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The repeater does not have an EmptyData template (assuming Rptr=repeater).

SO have a look here: http://devcenter.auburnrandall.com/EmptyRepeaterData.aspx

Comments

1

The select method returns a DataView object, you can check the Count propertie to se if any records have been returned:

SqlDataSource dsProdDocsHeader = new SqlDataSource(utils.Conn(Server.MachineName), SQL_PROD_DOCS);
DataView view = (DataView)dsProdDocsHeader.Select(DataSourceSelectArguments.Empty);

    if (view.Count == 0)
    {
        lblMessage.Text = "Message!!!";
    }
    else
    {
        RptrProductDocs.DataSource = view;
        RptrProductDocs.DataBind();
    }

Comments

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.