Prompt no data when there no data in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssmeshack
    New Member
    • Jul 2008
    • 38

    Prompt no data when there no data in gridview

    Hi All,

    Im using VWD 2008 and C#. Im using the code for search data. The seaching is success and no error. But here I want to add is, when the gridview return null value (which means there is no data from search query)...
    I want to show label "No Data".

    I tried to add this code to the code below:

    Code:
    if (gvTicket.DataSource == null)
            //{if (GridView1.DataSource == null)
            {
                lbNodata.Visible = true;
                Label6.Visible = false;
                gvTicket.Visible = false;
                
            }
           else
           {
               gvTicket.Visible = true;
               Label6.Visible = true;
           }

    Code:
    string connectionString = "Data Source=(local);Initial Catalog=DBhelpdesk;  User ID=;Password=";
            SqlConnection myConnection = new SqlConnection(connectionString);
            // SELECT * FROM Ticket where " + strSqlParam
            string strSqlParam = "";
            if (CbExactWord.Checked)
            {
                strSqlParam = ddlistColumn.SelectedValue + "='" + txtSearchBox.Text.Trim() + "'";
            }
            else
            {
                strSqlParam = ddlistColumn.SelectedValue + " like '%" + txtSearchBox.Text.Trim() + "%'";
            }
    
            SqlCommand sql = new SqlCommand("SelectTicketSearch", myConnection);
            sql.CommandType = CommandType.StoredProcedure;
            sql.Parameters.AddWithValue("@strSqlParam", strSqlParam);
            //Response.Write(strSqlParam);
    
            SqlDataAdapter ad = new SqlDataAdapter(sql);
    
            DataSet ds = new DataSet();
            ad.Fill(ds, "Ticket");
    
            gvTicket.DataSource = ds;
            gvTicket.DataBind();
    
            if (gvTicket.DataSource == null)
            //{if (GridView1.DataSource == null)
            {
                lbNodata.Visible = true;
                Label6.Visible = false;
                gvTicket.Visible = false;
                
            }
           else
           {
               gvTicket.Visible = true;
               Label6.Visible = true;
           }
    Pls help me. Thanks in advance.

    -Meshack
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Testing for the DataSouce itself being null is not the correct way to do it. If the search returns no data the DataSource will not be null.
    You need to test the data returned itself.

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      An alternative is to user a data reader and then check whether it has rows. If it does bind the reader to the gridview, else display message; maybe JS using attributes add. As r035198x says, the key is to check whether data is returned.

      Comment

      • ssmeshack
        New Member
        • Jul 2008
        • 38

        #4
        Thanks r035198x and kenobewan,

        Can you tell me how to check whether data is returned to gridview? Im still a newbie and learning. Can you help me?

        Thanks again.

        -Meshack

        Comment

        Working...