Fox
Fox

Reputation: 891

ObjectDataSource Convert issue - Exception

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

Upvotes: 0

Views: 390

Answers (1)

rt2800
rt2800

Reputation: 3045

Change first param datatype to string as follows

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

Upvotes: 1

Related Questions