This is a migrated thread and some comments may be shown as answers.

RadGrid Delete, Input string

2 Answers 146 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joseph Smith
Top achievements
Rank 1
Joseph Smith asked on 28 Jun 2010, 05:48 PM
Hi,

When I click the delete button on my RadGrid, I get this error:


Input string was not in a correct format.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10161267
   System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) +238
   System.String.System.IConvertible.ToDecimal(IFormatProvider provider) +88
   System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +750
   System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +156
   System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +298
   System.Web.UI.WebControls.SqlDataSourceView.AddParameters(DbCommand command, ParameterCollection reference, IDictionary parameters, IDictionary exclusionList, String oldValuesParameterFormatString) +808
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteDelete(IDictionary keys, IDictionary oldValues) +343
   System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +4052080
   Telerik.Web.UI.GridTableView.PerformDelete(GridEditableItem editedItem, Boolean suppressRebind) +268
   Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +2406
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +185
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +142
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981


Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

I found that this data format string is the culprit:

<telerik:GridBoundColumn DataField="Price" DataFormatString="{0:c}" DataType="System.Decimal"
            HeaderText="Price" SortExpression="Price" UniqueName="Price">
        </telerik:GridBoundColumn>

Here is all of my code:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductCart.ascx.cs" Inherits="UserControls_Products_ProductCart" %> 
<%--<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default"></telerik:RadAjaxLoadingPanel> 
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">--%> 
 
<%--</telerik:RadAjaxPanel> 
--%> 
 
<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True"  
    AllowAutomaticInserts="True" AllowAutomaticUpdates="True"  
    AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"  
    DataSourceID="SqlDataSource1" GridLines="None"
<MasterTableView AutoGenerateColumns="False" DataKeyNames="CartID,ProductID"  
        DataSourceID="SqlDataSource1"
<RowIndicatorColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
    <Columns> 
        <telerik:GridBoundColumn DataField="CartID" HeaderText="CartID" ReadOnly="True"  
            SortExpression="CartID" UniqueName="CartID" Visible="false"
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="ProductID" HeaderText="ProductID"  
            ReadOnly="True" SortExpression="ProductID" UniqueName="ProductID" Visible="false">        </telerik:GridBoundColumn> 
 
              <telerik:GridDropDownColumn DataField="ProductID"  
            DataSourceID="ObjectDataSource1" HeaderText="Product"  
            ListTextField="Name" ListValueField="ID" UniqueName="procolumn1"
        </telerik:GridDropDownColumn> 
        <telerik:GridBoundColumn DataField="Price" DataFormatString="{0:c}" DataType="System.Decimal"  
            HeaderText="Price" SortExpression="Price" UniqueName="Price"
        </telerik:GridBoundColumn> 
     
        <telerik:GridBoundColumn DataField="Quantity" DataType="System.Int32"  
            HeaderText="Quantity" SortExpression="Quantity" UniqueName="Quantity"
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="TimeStamp" DataType="System.DateTime"  
            HeaderText="TimeStamp" SortExpression="TimeStamp" UniqueName="TimeStamp"
        </telerik:GridBoundColumn> 
    </Columns> 
</MasterTableView> 
</telerik:RadGrid> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server"  
    ConflictDetection="CompareAllValues"  
    ConnectionString="<%$ ConnectionStrings:WilliamsFireConnectionString %>"  
    DeleteCommand="DELETE FROM [product_cart] WHERE [CartID] = @original_CartID AND [ProductID] = @original_ProductID AND [Price] = @original_Price AND [Quantity] = @original_Quantity AND [TimeStamp] = @original_TimeStamp"  
    InsertCommand="INSERT INTO [product_cart] ([CartID], [ProductID], [Price], [Quantity], [TimeStamp]) VALUES (@CartID, @ProductID, @Price, @Quantity, @TimeStamp)"  
    OldValuesParameterFormatString="original_{0}"  
    SelectCommand="SELECT * FROM [product_cart] WHERE ([CartID] = @CartID)"  
    UpdateCommand="UPDATE [product_cart] SET [Price] = @Price, [Quantity] = @Quantity, [TimeStamp] = @TimeStamp WHERE [CartID] = @original_CartID AND [ProductID] = @original_ProductID AND [Price] = @original_Price AND [Quantity] = @original_Quantity AND [TimeStamp] = @original_TimeStamp"
    <DeleteParameters> 
        <asp:Parameter Name="original_CartID" Type="String" /> 
        <asp:Parameter Name="original_ProductID" Type="String" /> 
        <asp:Parameter Name="original_Price" Type="Decimal" /> 
        <asp:Parameter Name="original_Quantity" Type="Int32" /> 
        <asp:Parameter Name="original_TimeStamp" Type="DateTime" /> 
    </DeleteParameters> 
    <InsertParameters> 
        <asp:Parameter Name="CartID" Type="String" /> 
        <asp:Parameter Name="ProductID" Type="String" /> 
        <asp:Parameter Name="Price" Type="Decimal" /> 
        <asp:Parameter Name="Quantity" Type="Int32" /> 
        <asp:Parameter Name="TimeStamp" Type="DateTime" /> 
    </InsertParameters> 
    <SelectParameters> 
        <asp:SessionParameter Name="CartID" SessionField="WilliamsFireShop_CartID"  
            Type="String" /> 
    </SelectParameters> 
    <UpdateParameters> 
        <asp:Parameter Name="Price" Type="Decimal" /> 
        <asp:Parameter Name="Quantity" Type="Int32" /> 
        <asp:Parameter Name="TimeStamp" Type="DateTime" /> 
        <asp:Parameter Name="original_CartID" Type="String" /> 
        <asp:Parameter Name="original_ProductID" Type="String" /> 
        <asp:Parameter Name="original_Price" Type="Decimal" /> 
        <asp:Parameter Name="original_Quantity" Type="Int32" /> 
        <asp:Parameter Name="original_TimeStamp" Type="DateTime" /> 
    </UpdateParameters> 
</asp:SqlDataSource> 
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"  
    SelectMethod="GetAllProducts" TypeName="WilliamsProducts.WilliamsProducts"
</asp:ObjectDataSource> 
 

This also happens with the Numeric Column type. What can I do to resolve this error without removing the data format string?

2 Answers, 1 is accepted

Sort by
0
Joseph Smith
Top achievements
Rank 1
answered on 28 Jun 2010, 06:12 PM
Hi,

When I used a LinqDataSource, everything works fine. The issue is with the data format string and the sql data source.
0
Tsvetoslav
Telerik team
answered on 01 Jul 2010, 09:32 AM
Hi Joseph,

What is actually happening in your case is that the grid is passing to the Price delete parameter an object of type string, containing a currency symbol, which the SqlDataSource tries to convert to the parameter's type - Decimal, and fails. Since RadGrid automatically populates the delete parameters of the SqlDataSource with the text values in the column cells of the item being delete, what you need to do is format in advance the text value of the problematic fields in the ItemCommand event as shown in the attached sample.

Hope it helps.

Regards,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Joseph Smith
Top achievements
Rank 1
Answers by
Joseph Smith
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or