OnRowUpdating Gridview输入字符串的格式不正确

时间:2014-04-09 16:18:53

标签: asp.net gridview

我无法想象我的生活......我有一个附加了sqldatasource的gridview,我在editItemtemplate格式化fee_amt<%#Bind(" fee_amt",&#34 ; {0:n}")%>像这样。通过单击更新lnk按钮引发OnRowUpdating事件时,我收到此错误。另请注意,我使用" $"格式化文本框中的数字。喜欢" $ 200.00"。

我很确定问题来自fee_amt,但我可能错了。

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) +9592843
System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)  +146
System.Convert.ToDecimal(String value, IFormatProvider provider) +67
System.String.System.IConvertible.ToDecimal(IFormatProvider provider) +10
System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +491
System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +126
System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63
System.Web.UI.WebControls.SqlDataSourceView.AddParameters(DbCommand command, ParameterCollection reference, IDictionary parameters, IDictionary exclusionList, String oldValuesParameterFormatString) +550
System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +346
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +95
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1226
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +855
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +121
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563




 Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)      Handles GridViewDegfaultCharges.RowUpdating

    'OnRowUpdating Sets Update Params for the GridView

    Dim TextBox2 As TextBox    'category
    Dim TextBox3 As TextBox    'fee_name
    Dim TextBox4 As TextBox    'fee_amt
    Dim CheckBox1 As CheckBox  'inactive
    Dim blnValid As Boolean
    lblMessage.Text = ""
    blnValid = True

    TextBox2 = GridViewDegfaultCharges.Rows(e.RowIndex).FindControl("TextBox2")
    TextBox3 = GridViewDegfaultCharges.Rows(e.RowIndex).FindControl("TextBox3")
    TextBox4 = GridViewDegfaultCharges.Rows(e.RowIndex).FindControl("TextBox4")
    CheckBox1 = GridViewDegfaultCharges.Rows(e.RowIndex).FindControl("CheckBox1")



    If CheckDefaultChargesCategory(TextBox2.Text) Then
        lblMessage.Text = TextBox2.Text & " is not a valid Loan Type"
        blnValid = False
        e.Cancel = True
        Exit Sub
    End If



    SqlDataDefaultCharges.UpdateParameters("category").DefaultValue = TextBox2.Text
    SqlDataDefaultCharges.UpdateParameters("fee_name").DefaultValue = TextBox3.Text
    SqlDataDefaultCharges.UpdateParameters("fee_amt").DefaultValue = Convert.ToDecimal(TextBox4.Text).ToString()
    SqlDataDefaultCharges.UpdateParameters("investorID").DefaultValue = Convert.ToInt32(_pageID)
    SqlDataDefaultCharges.UpdateParameters("inactive").DefaultValue = CheckBox1.Checked

    If blnValid Then
        SqlDataDefaultCharges.Update()
    End If




End Sub

1 个答案:

答案 0 :(得分:0)

我可能错了,但我不确定你是如何处理这个价值的,然而," fee_amt"正被转换为小数,然后回到字符串?

<强>之前:

SqlDataDefaultCharges.UpdateParameters("fee_amt").DefaultValue = Convert.ToDecimal(TextBox4.Text).ToString()

<强>后:

SqlDataDefaultCharges.UpdateParameters("fee_amt").DefaultValue = Convert.ToDecimal(TextBox4.Text)

修改

除非当然这个值应该是string,在这种情况下使用TextBox4.Text就足够了。

基于 codingbiz 评论,如果textbox中添加了美元符号。 Convert.ToDecimal()无法使用{&#39; $&#39;不是数值。我以为你正在展示&#39; $&#39;在TextBox旁边。