pepster
pepster

Reputation: 35

How can I check for Zero rows from SQL SqlDataSource in C#?

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!

Upvotes: 1

Views: 1625

Answers (3)

IceCode
IceCode

Reputation: 1751

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();
    }

Upvotes: 1

Raghubar
Raghubar

Reputation: 2788

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

Upvotes: 2

Pleun
Pleun

Reputation: 8920

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

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

Upvotes: 1

Related Questions