user482375
user482375

Reputation:

Check if Method Returns Results

I have a method I call and after the method is called I need to see if it returns no results or results. If it returns results a certain panel is displayed and if no results a certain panel is displayed.

This is my method:

public DataView RedeemCoupon()
{
    string connStr = ConfigurationManager.ConnectionStrings["SiteSqlServer"]
                    .ConnectionString;
    SqlConnection conn = new SqlConnection(connStr);
    SqlCommand cmd = new SqlCommand("CPC_GetCoupons", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@CouponCode", txtCouponCode.Text));
    SqlDataAdapter dap = new System.Data.SqlClient.SqlDataAdapter(cmd);
    DataSet ds = new DataSet();

    // open conn
    if (conn.State == ConnectionState.Closed)
        conn.Open();

    // fill
    dap.Fill(ds);

    // close the conn
    if (conn.State == ConnectionState.Open)
        conn.Close();

    return ds.Tables[0].DefaultView;
}

Any ideas?

Thanks!

Upvotes: 0

Views: 115

Answers (2)

Yuck
Yuck

Reputation: 50825

You can check the .Table.Rows property of DefaultView:

var dataView = RedeemCoupon();

if (dataView.Table.Rows.Count == 0) {
    // show the no results panel
}
else {
    // show another panel
}

Upvotes: 3

Digbyswift
Digbyswift

Reputation: 10400

You should just be able to check the Count property of the result, e.g.

int numberOfResults = RedeemCoupon().Count;

or

bool hasResults = RedeemCoupon().Count > 0

Upvotes: 2

Related Questions