title: how to display error message if dropdown list item doesnt have matching records in gridview?
body: <p>In aspx page i have: 2 dropdown lists(both databound and outside gridview) , one search button and GridView1. Im using stored procedure in sql. One dropdown is MainMenu and other is  category.If i select an item from MainMenu ,Category dropdown will load corresponding categories. When i click search button Gridview1 will show rows matching these selection. My problem: If there are no rows matching dropdown selections ,error msg should be displayed which is not displayed.i tried several ways but in vain. However there is no error. Pls help with the code.</p>
  
  <p>Stored Procedure:</p>
  
  <pre><code> ALTER  PROCEDURE  [dbo].[SearchStentRecords_Sp]
                  @Action VARCHAR(10)
                 ,@ddl1  VARCHAR(50)
                 ,@ddl2 VARCHAR(50)
                 , @ERROR int OUTPUT
  AS
  BEGIN
  IF EXISTS(SELECT * FROM Addstock where Addstock.Device=@ddl1)
  
  
      BEGIN 
         set @ERROR=1
          IF @Action = 'SELECT1'
              BEGIN
              SELECT * FROM Addstock where Addstock.Device=@ddl1 AND Addstock.Category=@ddl2 AND Used='N' AND TakenBack='N'
               END
  
      ELSE IF @Action = 'SELECT2'
                  BEGIN
                  SELECT * FROM Addstock where Addstock.Device=@ddl1 AND Used='N' AND TakenBack='N'
                 END
       END                   
     ELSE 
               BEGIN
               SET @ERROR= 0
               END
  
  END 
  </code></pre>
  
  <p>Code behind for search button click method:</p>
  
  <pre><code>protected void btnSearch_Click(object sender, EventArgs e)
          {
              string constr = WebConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
              using (SqlConnection con = new SqlConnection(constr))
              {
                  con.Open();
  
                  DataTable dt = new DataTable();
                  SqlCommand cmd = new SqlCommand();
                  SqlDataAdapter adp = new SqlDataAdapter();
                  try
                  {
  
  
                      if (ddlMainmenu.SelectedIndex != 0 &amp;&amp; ddlStentCat.SelectedIndex != 0)
                      {
  
                          cmd = new SqlCommand("SearchStentRecords_Sp", con);
                          cmd.CommandType = CommandType.StoredProcedure;
                          cmd.Parameters.AddWithValue("@Action", "SELECT1");
                          cmd.Parameters.AddWithValue("@ddl1", ddlMainmenu.SelectedItem.Text);
                          cmd.Parameters.AddWithValue("@ddl2", ddlStentCat.SelectedItem.Text);
                          SqlParameter resultParam= new SqlParameter("@ERROR", System.Data.SqlDbType.Int);
                          resultParam.Direction = System.Data.ParameterDirection.Output;
                          cmd.Parameters.Add(resultParam);
                          cmd.ExecuteNonQuery();
                          int retVal;
  
                          int.TryParse(resultParam.Value.ToString(),out retVal);
                          if (retVal == 1)
                          {
  
                              adp.SelectCommand = cmd;
                              adp.Fill(dt);
                              GridView1.DataSource = dt;
                              GridView1.DataBind();
                          }
                          else if(retVal==0)
                          {
                              GridView1.Visible=false;
                              lblErrormsg.Text = "No Records Found";
                          }
  
  
                      }
                      else if (ddlMainmenu.SelectedIndex != 0 &amp;&amp; ddlStentCat.SelectedIndex == 0)
                      {
  
  
                          cmd = new SqlCommand("SearchStentRecords_Sp", con);
                          cmd.CommandType = CommandType.StoredProcedure;
                          cmd.Parameters.AddWithValue("@Action", "SELECT2");
                          cmd.Parameters.AddWithValue("@ddl1", ddlMainmenu.SelectedItem.Text);
                          SqlParameter resultParam= new SqlParameter("@ERROR", System.Data.SqlDbType.Int);
                          resultParam.Direction = System.Data.ParameterDirection.Output;
                          cmd.Parameters.Add(resultParam);
                          cmd.ExecuteNonQuery();
                          int retVal;
                              //= (int)cmd.Parameters["@ERROR"].Value;
                          int.TryParse(resultParam.Value.ToString(),out retVal);
                          if (retVal==1)
                          {
                              adp.SelectCommand = cmd;
                              adp.Fill(dt);
                              GridView1.DataSource = dt;
                              GridView1.DataBind();
                          }
                          else if(retVal==0)
                          {
                              lblErrormsg.Text = "No Records Found";
                          }
  
                      }
  
  
                  }
                  catch (Exception ex)
                  {
                      ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
                  }
                  finally
                  {
                      con.Close();
  
                  }
              }
  
          }
  </code></pre>
  
  <p>for example. Suppose dropdown1 has items: Car,Bikes. And Car has subcategories: BMW,Honda City. Bikes doesnt hav subcategories. Because im adding Items to both dropdown lists through other aspx pages hence dropdown lists are databound. NOw when i select Car , 2nd dropdown ll load BMW,Hond City. Suppose i click BMW and search button--> gridview will show following:</p>
  
  <pre><code>MainCategory                    Category             Colour
  
      Car                        BMW                Black
  </code></pre>
  
  <p>And now if select  Bike and click search button Gridview remains same. It should display NO Match found because there is no subcategory added for bike hence no record.  My problem is when search button clicked, dropdown item Bike should match any row in gridview with Bike. how to check this? I agree it was easier if i deployed dropdown inside gridview but since 2nd dropdown is dependent on 1st i didnt go for it to avoid complication. this is my problem.</p>