编译器错误消息:CS0029:无法将类型'decimal'隐式转换为'string'

时间:2013-09-25 11:11:00

标签: asp.net

我使用GridView,我希望插入更新。我完成了我的工作但是当我使用RchPaidAmt字段和数字数据类型时,显示以下消息: 编译器错误消息:CS0029:无法将类型'decimal'隐式转换为'string'

数据类型

RchID                     int           
RchAppID                  int           
RchCallDate               datetime             
RchComments               varchar(150)       
RchAttemptCall            int           
RchReceivedParty          varchar(50)         
RchPrmTaken               int           
RchCNPrm                  int           
RchNxtPrmDate             datetime             
RchPaidAmt                numeric(18, 2)


<UpdateParameters>
                    <asp:Parameter Name="RchAppID" Type="Int32" />
                    <asp:Parameter Name="RchCallDate" Type="DateTime" />
                    <asp:Parameter Name="RchComments" Type="String" />
                    <asp:Parameter Name="RchAttemptCall" Type="Int32" />
                    <asp:Parameter Name="RchReceivedParty" Type="String" />
                    <asp:Parameter Name="RchPrmTaken" Type="Int32" />
                    <asp:Parameter Name="RchCNPrm" Type="Int32" />
                    <asp:Parameter Name="RchNxtPrmDate" Type="DateTime" />
              <asp:Parameter Name="RchPaidAmt" Type="Decimal" />   
</UpdateParameters>





protected void GVCallHistory_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Add")
        {
            string strRchAppID = hfRchAppID.Value;
            string strRchCallDate = DateTime.Parse(Convert.ToString(DateTime.Now)).ToString("dd MMM yyyy HH:MM");
            string strRchComments = ((e.CommandSource as Button).NamingContainer.FindControl("txtRchComments") as TextBox).Text;
            string strRchAttemptCall = ((e.CommandSource as Button).NamingContainer.FindControl("txtRchAttemptCall") as TextBox).Text;            
            string strRchReceivedParty = ((e.CommandSource as Button).NamingContainer.FindControl("DDLRchReceivedParty") as DropDownList).SelectedValue;
            string strRchPrmTaken = ((e.CommandSource as Button).NamingContainer.FindControl("txtRchPrmTaken") as TextBox).Text;
            string strRchCNPrm = ((e.CommandSource as Button).NamingContainer.FindControl("txtRchCNPrm") as TextBox).Text;
            string strRchNxtPrmDate = "";
            if (Convert.ToString(((e.CommandSource as Button).NamingContainer.FindControl("txtRchNxtPrmDate") as TextBox).Text) !="")
            {
                strRchNxtPrmDate = DateTime.Parse(Convert.ToString(((e.CommandSource as Button).NamingContainer.FindControl("txtRchNxtPrmDate") as TextBox).Text)).ToString("dd MMM yyyy");
            }
            else
            {
                strRchNxtPrmDate = "01Jan1900";
            }

            **string strRchPaidAmt = ((e.CommandSource as Button).NamingContainer.FindControl("txtRchPaidAmt") as TextBox).Text;**

            DSCallHistory.InsertParameters["RchAppID"].DefaultValue = DateTime.Parse(Convert.ToString(DateTime.Now)).ToString("dd MMM yyyy HH:MM");// strRchAppID;
            DSCallHistory.InsertParameters["RchCallDate"].DefaultValue = strRchCallDate;
            DSCallHistory.InsertParameters["RchComments"].DefaultValue = strRchComments;
            DSCallHistory.InsertParameters["RchAttemptCall"].DefaultValue = strRchAttemptCall;
            DSCallHistory.InsertParameters["RchReceivedParty"].DefaultValue = strRchReceivedParty;
            DSCallHistory.InsertParameters["RchPrmTaken"].DefaultValue = strRchPrmTaken;
            DSCallHistory.InsertParameters["RchCNPrm"].DefaultValue = strRchCNPrm;
            DSCallHistory.InsertParameters["RchNxtPrmDate"].DefaultValue = strRchNxtPrmDate;

            **if (strRchPaidAmt !=null)
            {
                DSCallHistory.InsertParameters["RchPaidAmt"].DefaultValue = Convert.ToDecimal(strRchPaidAmt);
            }
            else
            {
                DSCallHistory.InsertParameters["RchPaidAmt"].DefaultValue ="0";
            }**
            DSCallHistory.Insert();
        }
    }



what should I do. Pls suggest me with best solution.

Thanks
Nur
  

其他信息

 <asp:SqlDataSource ID="DSCallHistory" runat="server" 
                ConflictDetection="CompareAllValues" 
                ConnectionString="<%$ ConnectionStrings:OptimaWebCustomerQueryCon %>" 
                DeleteCommand="DELETE FROM [TblRecCallHistory] WHERE [RchID] = @RchID" 
                InsertCommand="INSERT INTO [TblRecCallHistory] ([RchAppID], [RchCallDate], [RchComments], [RchAttemptCall], [RchReceivedParty], [RchPrmTaken], [RchCNPrm], [RchNxtPrmDate], [RchPaidAmt]) VALUES (@RchAppID, @RchCallDate, @RchComments, @RchAttemptCall, @RchReceivedParty, @RchPrmTaken, @RchCNPrm, @RchNxtPrmDate, @RchPaidAmt)" 
                OldValuesParameterFormatString="original_{0}" 
                SelectCommand="SELECT [RchID], [RchAppID], [RchCallDate], [RchComments], [RchAttemptCall], [RchReceivedParty], [RchPrmTaken], [RchCNPrm], 
                [RchNxtPrmDate]=(case when [RchNxtPrmDate] = '01Jan1900' then NULL else [RchNxtPrmDate] end),
                [RchPaidAmt] FROM [TblRecCallHistory] WHERE ([RchAppID] = @RchAppID) ORDER BY [RchID]" 
                UpdateCommand="UPDATE [TblRecCallHistory] SET [RchAppID] = @RchAppID, [RchCallDate] = @RchCallDate, [RchComments] = @RchComments, [RchAttemptCall] = @RchAttemptCall, [RchReceivedParty] = @RchReceivedParty, [RchPrmTaken] = @RchPrmTaken, [RchCNPrm] = @RchCNPrm, [RchNxtPrmDate] = @RchNxtPrmDate, [RchPaidAmt] = @RchPaidAmt WHERE [RchID] = @RchID">
                <DeleteParameters>
                    <asp:Parameter Name="RchID" Type="Int32" />

                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="RchAppID" Type="Int32" />
                    <asp:Parameter Name="RchCallDate" Type="DateTime" />
                    <asp:Parameter Name="RchComments" Type="String" />
                    <asp:Parameter Name="RchAttemptCall" Type="Int32" />
                    <asp:Parameter Name="RchReceivedParty" Type="String" />
                    <asp:Parameter Name="RchPrmTaken" Type="Int32" />
                    <asp:Parameter Name="RchCNPrm" Type="Int32" />
                    <asp:Parameter Name="RchNxtPrmDate" Type="DateTime" />
                    <asp:Parameter Name="RchPaidAmt" Type="Decimal" />
                </InsertParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="GVRecList" Name="RchAppID" 
                        PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="RchAppID" Type="Int32" />
                    <asp:Parameter Name="RchCallDate" Type="DateTime" />
                    <asp:Parameter Name="RchComments" Type="String" />
                    <asp:Parameter Name="RchAttemptCall" Type="Int32" />
                    <asp:Parameter Name="RchReceivedParty" Type="String" />
                    <asp:Parameter Name="RchPrmTaken" Type="Int32" />
                    <asp:Parameter Name="RchCNPrm" Type="Int32" />
                    <asp:Parameter Name="RchNxtPrmDate" Type="DateTime" />
                    <asp:Parameter Name="RchPaidAmt" Type="Decimal" />
                </UpdateParameters>
            </asp:SqlDataSource>

3 个答案:

答案 0 :(得分:0)

DSCallHistory.InsertParameters["RchPaidAmt"].DefaultValue ="0";

应该是:

DSCallHistory.InsertParameters["RchPaidAmt"].DefaultValue =0m;

答案 1 :(得分:0)

在将decimal值分配给DefaultValue之前,请勿将其转换为DefaultValue期望字符串

DSCallHistory.InsertParameters["RchPaidAmt"].DefaultValue = strRchPaidAmt;

答案 2 :(得分:0)

更改以下代码行

DSCallHistory.InsertParameters["RchPaidAmt"].DefaultValue = Convert.ToDecimal(strRchPaidAmt);

是:

DSCallHistory.InsertParameters["RchPaidAmt"].DefaultValue = Convert.ToDecimal(strRchPaidAmt).ToString();

问题是您正在尝试将小数设置为字符串对象。

编辑:Jame的解决方案可能更合适。