2 Replies - 3617 Views - Last Post: 04 April 2011 - 08:39 AM Rate Topic: -----

#1 krista80  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 22-February 11

Using an If statement with a Gridview

Posted 04 April 2011 - 06:54 AM

I have GridView that uses a DataSet as it's Data Source. What I want to do is when this GridView or Data Source are empty, a label will change into an error message informing the user that there was nothing to post. My code currently looks like this:
private void PostResultsFull()
        {
            string sqlPFQuery = string.Empty;
            StringBuilder sbPF = new StringBuilder();

            sbPF.AppendFormat(string.Format(" SELECT BU, P, Bus, CC, A "));
            sbPF.AppendFormat(string.Format(" FROM VResults "));
            sbPF.AppendFormat(string.Format(" WHERE VResults.SessionID IN (SELECT SessionID FROM VTemp) "));
            sbPF.AppendFormat(string.Format(" AND "));
            sbPF.AppendFormat(string.Format(" A IS NOT NULL "));

            sqlPFQuery = sbPF.ToString();

            using (SqlConnection sqlConn = new SqlConnection(connectionString))
            {
                sqlConn.Open();

                SqlCommand sqlPFCmd = new SqlCommand(sqlPFQuery, sqlConn);

                SqlDataAdapter adapter = new SqlDataAdapter(sqlPFCmd);
                DataSet ds = new DataSet();//makes a new data set
                adapter.Fill(ds);//fills the data set with the query results
                grdResultsFull.Visible = true;//makes the GridView control visible
                grdResultsFull.DataSource = ds;//makes the GridView control's data source the data set
                grdResultsFull.DataBind();//binds the data to the GridView control, formatting the GridView
                if (grdResultsFull.DataSource == "")
                {
                    lblError.Text = "An error occurred in the last page you attended. Please return to the last page and try again.";
                }
                else
                {
                    ds.Dispose();
                    adapter.Dispose();
                    sqlConn.Close();
                }
            }
            lblFull.Visible = true;
            lblFull.Text = "Invalid Cost Centre Codes";
        }

I've obviously have the if in the wrong place and the code inside the if is also most likely wrong. So the questions are, where should I put this if statement, and also what do I put into it to make it work the way I want it to?

Should it be in the Page_Load section maybe?
  protected void Page_Load(object sender, EventArgs e)
        {
            RemoveBlankCC();
            PostResultsFull();
            PostResultsMissing();
            TruncateTemp();
        }

Any help is of course appreciated. Again, I just want the error message to appear if the gridview is empty/has nothing to post.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Using an If statement with a Gridview

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Using an If statement with a Gridview

Posted 04 April 2011 - 07:41 AM

You should be able to use the EmptyDataText attribute of the GridView to show some text when the DataSource contains no rows. So that IF statement may not be needed.

http://msdn.microsof...tydatatext.aspx
Was This Post Helpful? 1
  • +
  • -

#3 krista80  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 22-February 11

Re: Using an If statement with a Gridview

Posted 04 April 2011 - 08:39 AM

Perfect! Thank you, eclipsed4utoo!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1