1

I have an ObjectDataSource with a parameter from a query string. I'm getting the following error:

Value was either too large or too small for an Int32.

I believe the query string might be too long for it's datatype. The InfoSheetID is populated from the database. Is there another option I can take or modify the ObjectDataSource to prevent the error?

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetInfoByInfoID" 
        TypeName="BLL.InfoViewBLL">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="2148663911" Name="InfoSheetID" 
                QueryStringField="InfoSheetID" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
1
  • As well as potential length issues, consider how you might want to handle an arbitrary non-integer value entered by the user. For example 267.52 or "hello". In most cases this won't be an issue, but blindly casting values will lead to exceptions in these situations and the query string is particularly easy to alter. Commented Mar 9, 2013 at 22:40

1 Answer 1

2

Int32.MaxValue is 2'147'483'647, that is lower than 2'148'663'911 which you use.

You can change type to Int64 or refactore your code to avoid using so big values.

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetInfoByInfoID" 
        TypeName="BLL.InfoViewBLL">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="2148663911" Name="InfoSheetID" 
                                                    ^^^^^^^^^^
                QueryStringField="InfoSheetID" Type="Int64" />
                                                     ^^^^^
        </SelectParameters>
    </asp:ObjectDataSource>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I tried that and got the following error.Object of type 'System.Int64' cannot be converted to type 'System.Int32'. In the configure datasource, when asked to choose a method, It only has GetInfoByInfoID(Int32 InfoSheetID), returns InfoDataTable option available. No Int64.
The mistake is that DefaultValue="2148663911". Make sure, that you really need this number. Also you can use string instead of Int32 and make an int.TryParse() for the GetInfoByInfoID() method. Anyway I would prefer to see the default value == 0 or -1 depending on the task.
I only put the default value to show the numbers of the querystring. It is removed with no default value. I will try TryParse()
It looks like a DataBase uses long for Id. Can you update the GetInfoByInfoID() method to receive int64?
I got it working. I changed the Public Function in my business logic class to (ByVal InfoSheetID As Int64) As InfoDataTable. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.