在下面的程序里面,
string  salePrice = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string discount = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
这里的 salePrice discount 在数据库里面对应 salePrice discount 类型是numeric ,我想我用string 是错的吧,
不知道应该用什么呢!??
其他的用不用改一下呢!!!希望讲解一下。。using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;namespace WebTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {        }        public void getData()
        {
            SqlConnection cn = new SqlConnection("server=localhost;database=student;user=xie;password=xie");
            SqlCommand cmd = new SqlCommand("select*from goods", cn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "goods");
            this.GridView1.DataSource = ds.Tables[0].DefaultView;
            this.DataBind();
        }        protected void Button1_Click(object sender, EventArgs e)
        {
            getData();
        }        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            getData();
        }        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {        }        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            getData();
        }        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string goodsName = this.GridView1.DataKeys[e.RowIndex][0].ToString();
            string  salePrice = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
            string discount = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
            Response.Write("afdasfd");
            updataGoods(goodsName,salePrice ,discount);
            this.GridView1.EditIndex = -1;
            getData();
        }        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            getData();
        }
        public void updataGoods(string goodsName,string  salePrice, string   discount)
        {
            SqlConnection cn = new SqlConnection("server=localhost;database=student;user=xie;password=xie");
            SqlCommand cmd = new SqlCommand("update goods set goodsName=' " +goodsName+" ', salePrice=' " +salePrice+" ', discount=' " +discount+" ' where goodsName=' "+goodsName+" ' ", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
    }
}

解决方案 »

  1.   

    用String 没错 但是你保证他可以转换成数字 比如说空格就不行
    在sql中不用加单引号
    SqlCommand cmd = new SqlCommand("update goods set goodsName=' " +goodsName+" ', salePrice= " +salePrice+" , discount= " +discount+" where goodsName=' "+goodsName+" ' ", cn);
      

  2.   

    你用string试试啊不用string用什么?
      

  3.   

    cast(salePrice as varchar(50)
    cast(discount as varchar(50)
    转化为字符串格式的