TaroYuki
TaroYuki

Reputation: 147

Gridview databinding issue after clear out search box

I am using a sqldatasource to bind the gridview. If I click on the search button, I will using the following code to rebind the gridview with different stored procedure sqldatasource. If i clear out the search box and click the search again, i would like the gridview to rebind to the original datasource. But I always get the error "Procedure sp_get_ecr_list has no parameters and arguments were supplied." Any thoughts?

       if (searchbox.Value.Trim() != "")
        {

            SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            SqlDataSource1.SelectCommand = "sp_get_ecr_list_filter";
            Parameter d = SqlDataSource1.SelectParameters["devNumber"];
            SqlDataSource1.SelectParameters.Remove(d);
            SqlDataSource1.SelectParameters.Add("devNumber", searchbox.Value);
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataBind();
        }

Upvotes: 1

Views: 79

Answers (2)

Martin Parenteau
Martin Parenteau

Reputation: 73761

You could clear the parameter list in your else clause before binding the data:

if (!string.IsNullOrWhiteSpace(searchbox.Value))
{
    ...
    SqlDataSource1.SelectParameters.Add("devNumber", searchbox.Value);
}
else
{
    SqlDataSource1.SelectCommand = "sp_get_ecr_list"; // Probably already set earlier in your code
    SqlDataSource1.SelectParameters.Clear();
}

GridView1.DataBind();

Upvotes: 2

DNKROZ
DNKROZ

Reputation: 2872

So are you saying that when you clear the search box and click enter, your equality comparison isn't working as you intended and therefore dropping into the if statement?

Try this instead:

if (string.IsNullOrEmpty(searchbox.Value)) 
  {
    // Data bind
  }

Upvotes: 0

Related Questions