Chidi Okeh
Chidi Okeh

Reputation: 1557

Input string was not in a correct format error when passing values of decimal data type as querystring

We have the following field names in our database with data type of decimal:

RentalFee
ExtraHourFee
CancelFee
KeyDeposit

When attempting to pass their values as querystring from one page to another, we run into Input string was not in a correct format error.

Here is a snippet of the markup:

<asp:TemplateField HeaderText="Select" SortExpression="siteid">
  <ItemTemplate>
  <asp:HyperLink ID="hdReserve" Text="Select" runat="server" 
        NavigateUrl='<%# "Reserve.aspx?id=" + Eval("siteId") + "&groupsize=" + ddlPartySize.SelectedValue + "&facilityFees= " + Eval("RentalFeeAmount") + "&facilityFees= " + Eval("RentalFeeAmount") + "&depoitAmt= " + Eval("DepositAmount") + "&cancelAmt= " + Eval("CancellationAmount") + "&keydeptAmt= " + Eval("KeyDepositAmount") %>' /> 
  </ItemTemplate> 
</asp:TemplateField>

Then the values are grabbed from codebehind:

        Dim intRentalFee As Decimal
        Dim intExtraHourFee As Decimal
        Dim intCancelFee As Decimal
        Dim intKeyDeposit As Decimal

        rentalfeeHide.Text = Request.QueryString("facilityfees")
    extrahrfeeHide.Text = Request.QueryString("extrahour")
    cancelfeeHide.Text = Request.QueryString("cancelAmt")
        keydepositfeeHide.Text = Request.QueryString("keydeptAmt")


        intRentalFee = rentalfeeHide.Text
        intExtraHourFee = extrahrfeeHide.Text
        intCancelFee = cancelfeeHide.Text
        intKeyDeposit = keydepositfeeHide.Text

 ' Add all up to get total fee
lblTotal.Text = intRentalFee + intExtraHourFee + intCancelFee + intKeyDeposit

Any ideas how to resolve this?

Upvotes: 0

Views: 490

Answers (3)

Mych
Mych

Reputation: 2563

Do in in code behind...

hdReserve.NavigateUrl = string.Format("../Reserve.aspx?id=?id={0}&groupsize={1}&facilityFees={2}...", siteId, ddlPartySize.SelectedValue, intExtraHourFee...)

Upvotes: 1

hutchonoid
hutchonoid

Reputation: 33306

I would use the following instead as it is very difficult to see what is happening:

 <asp:hyperlinkfield datatextfield="UnitPrice"
            datanavigateurlfields="siteId,groupsize,facilityFees"
            datanavigateurlformatstring="~/details.aspx?siteId={0}&groupsize={1}&facilityFees={2}"   />

The above only demonstrates a few fields but it uses datanavigateurlformatstring for the url and datanavigateurlfields for the arguments which can be specified with a comma-separated string.

MSDN Hyperlink reference

You should then be able to clearly see in the url what the values are and check that they match your intended type for the destination page and convert them i.e.

var facilityfees = Convert.ToDecimal(Request.QueryString("facilityfees"));

Upvotes: 2

Mych
Mych

Reputation: 2563

In addition to Huchanoid's answer you need to make some mods to your codebehind.

First off you say the values are decimal but you are using them as integers? Which are they?

Next, what is the querystrings don't exists... someone just types in the page name... I would say you need to check that these values exist and then cast them as integers (or Decimal)... Use

 If Not IsNothing(Request.QueryString("yourQueryStringName")) Then
     'check that they are numeric or use a try catch... 
     If isNumeric(Request.QueryString("yourQueryStringName")) Then
        'Querystring is Numeric
           intYourQueryStringName = Cint(Request.QueryString("yourQueryStringName"))
     Else
         'Error Querystring is not numeric
         Response.Redirect("Somwhere.aspx")
     End If
 Else
     'QueryString does not exist
     Response.Redirect("Somwhere.aspx")
 End If

Upvotes: 0

Related Questions