当前位置:编程学习 > C#/ASP.NET >>

无法将 char 值转换为 money。该 char 值的语法有误。

sql 数据表结构

Typeid     int  

TypeName    navrchar(50)

TypePrice      Money

AddBedPrice  Money

IsAddBed     nchar(10)

remark         navrchar(100)



采用的是三层结构 

实体类 

   public class RoomType
    {
       public int TypeID { get; set; }
       public string TypeName { get; set; }
       public decimal TypePrice { get; set; }
       public decimal  AddBedPrice { get; set; }
       public string IsAddBed { get; set; }
       public string Remark { get; set; }
    }
   DAL层
 public class RoomTypeSerivce{
     public static int UpdateRoomType(RoomType roomtype) 
       {
           string sql = "update roomtype set TypeName=@TypeName,TypePrice=@TypeName,AddBedPrice=@AddBedPrice,IsAddBed=@IsAddBed,Remark=@Remark where TypeID=@TypeID";
           SqlParameter[] pams = new SqlParameter[6];
           pams[0] = new SqlParameter("@TypeID", roomtype.TypeID);
           pams[1] = new SqlParameter("@TypeName", roomtype.TypeName);
           pams[2] = new SqlParameter("@TypePrice", roomtype.TypePrice);
           pams[3] = new SqlParameter("@AddBedPrice", roomtype.AddBedPrice);
           pams[4] = new SqlParameter("@IsAddBed", roomtype.IsAddBed);
           pams[5] = new SqlParameter("@Remark", roomtype.Remark);
           return sqlhepler.ExecuteCommand(sql, pams);
       }
}
   Bll层

   public static int UpdateRoomType(RoomType roomtype) 
       {
           return RoomTypeSerivce.UpdateRoomType(roomtype);
       }

页面 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdateRomType.aspx.cs" Inherits="UpdateRomType" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td>
                房间类型</td>
            <td>
                <asp:TextBox ID="txttype" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                房间价格</td>
            <td>
                <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                是否加床</td>
            <td>
                <asp:RadioButton ID="RadioButton1" runat="server" Text="是" GroupName="Radio" />
                <asp:RadioButton ID="RadioButton2" runat="server" Text="否" GroupName="Radio" />
            </td>
        </tr>
        <tr>
            <td>
                备注</td>
            <td>
                <asp:TextBox ID="txtRemark" runat="server" Height="76px" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                加床价格</td>
            <td>
                <asp:TextBox ID="txtAddBedPrice" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:Button ID="Button1" runat="server" Text="确定" onclick="Button1_Click"  />
            </td>
        </tr>
    </table>
    <div>
    
    </div>
    </form>
</body>
</html>


后台代码 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HotelBll;
using HotelModels;

public partial class UpdateRomType : System.Web.UI.Page
{
    int id;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Convert.ToInt32(Request.QueryString["typeid"]);
        if (!IsPostBack) 
        {
           
            Response.Write(id);
            RoomType roomtype = RoomTypeManage.GetRoomTypeById(id);
            this.txttype.Text = roomtype.TypeName;
            this.txtprice.Text = roomtype.TypePrice.ToString();
            this.txtRemark.Text = roomtype.Remark;
            this.txtAddBedPrice.Text = roomtype.AddBedPrice.ToString();
            if (roomtype.IsAddBed.Trim() == "是".Trim())
            {
                this.RadioButton1.Checked = true;
            }
            else 
            {
                this.RadioButton2.Checked = true;
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //if (Update() > 0) 
        //{
        //    Response.Write("<script language='javascript'>alert('更新成功')</script>");
        //} 

        Update();
    }
    public void   Update() 
    {
        RoomType roomtype = new RoomType();
        roomtype.TypeID = id;
        roomtype.TypeName = this.txttype.Text;
        roomtype.TypePrice = Convert.ToDecimal(this.txtprice.Text);
        roomtype.AddBedPrice =Convert.ToDecimal(this.txtAddBedPrice.Text);
        
        if (this.RadioButton1.Checked)
        {
            roomtype.IsAddBed = "是";
        }
        else 
        {
            roomtype.IsAddBed = "否";
        }
        roomtype.Remark = this.txtRemark.Text;
        int a = RoomTypeManage.UpdateRoomType(roomtype);


        Response.Redirect("RoomType.aspx");
    }
}


出现无法将 char 值转换为 money。该 char 值的语法有误。


我检查了参数 以及类型转换。检查不出,望高手给与解答,谢谢啊!
--------------------编程问答-------------------- may be have null value...
check the following:

 roomtype.TypePrice = Convert.ToDecimal(this.txtprice.Text);
 roomtype.AddBedPrice =Convert.ToDecimal(this.txtAddBedPrice.Text); --------------------编程问答-------------------- SqlParameter   para =   new   SqlParameter( "@AddBedPrice",SqlDbType.Money,8); 
cmd.Parameters.Add(para ); 
单步看看值 --------------------编程问答--------------------
引用 1 楼 fangxinggood 的回复:
may be have null value...
check the following:

 roomtype.TypePrice = Convert.ToDecimal(this.txtprice.Text);
 roomtype.AddBedPrice =Convert.ToDecimal(this.txtAddBedPrice.Text);


 不为空的 我调试了很久的! --------------------编程问答--------------------
引用 2 楼 wuyq11 的回复:
SqlParameter para = new SqlParameter( "@AddBedPrice",SqlDbType.Money,8); 
cmd.Parameters.Add(para ); 
单步看看值



也不行啊。。  纠结,我调试的结果是,前面这些值都是decimal类型的,怎么一调用数据访问层的方法就变成char的值了!
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,