Question

I have a Gridview control which I want to bind dynamically based on some controls on my page. I've gotten everything to work fine except for the following code where i'm trying to dynamically set the Select Method as well pass the parameters. I'm getting a exception and I've tried multple ways of creating the Parameter. Herewith the code:

string filterValue = FilterValue.Value; //FilterValue.Value is a HiddenField (Value is definitely populated)

MyObjDataSource.SelectParameters.Add("policynumber",TypeCode.String, filterValue);    
MyObjDataSource.SelectMethod = "GetPolicybyPolicyNumber";    
MyObjDataSource.Select();

My Select Method looks like so:

public IQueryable GetPolicybyPolicyNumber(Parameter policynumber, string sortExpr, int maximumRows, int startRowIndex)
    {
       //Debugging does not even reach here. I'm just returning null for demo purposes
       return null;
    }

The exception I keep on getting is :

System.InvalidOperationException: Cannot convert value of parameter 'policynumber' from 'System.String' to 'System.Web.UI.WebControls.Parameter'
at System.Web.UI.WebControls.ObjectDataSourceView.ConvertType(Object value, Type type, String paramName) ....

I have also tried this, which gives the same exception:

    Parameter param = new Parameter();
    param.Type = TypeCode.String;
    param.DefaultValue = FilterValue.Value;
    MyObjDataSource.SelectParameters.Add(param);

Any advice? Tx

Was it helpful?

Solution

Change first param datatype to string as follows

public IQueryable GetPolicybyPolicyNumber(string policynumber, string sortExpr, int maximumRows, int startRowIndex){...}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top