|登录 |注册

查看: 2038|回复: 4
打印 上一主题 下一主题

我的asp.net c# coding有问题,帮帮忙!

 关闭 [复制链接]
karen_er30
2010-9-8 06:16 PM
public partial class Inventory_ABCAnalysis : System.Web.UI.Page
{
   

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string testing(int i)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connString"]);
        string sql = "SELECT COUNT[sellingcost] AS total FROM [Item]";

        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        cmd.CommandType = CommandType.Text;

        da.Fill(ds, "Item");
        dt = ds.Tables["Item"];
        
       string category;
        int total;
        total = 100 / dt.Rows[0]["total"];

        if (total * i <= 20)
            category = "A";
        else if (total * i <= 50)
            category = "B";
        else if (total * i <= 100)
            category = "C";
        return category;

    }

}


当我run时,出现以下错误。
Error: Operator '/' cannot be applied to operands of type 'int' and 'object'

由于我一直不能解决这问题,所以才请问各位帮帮忙了,在此感激不尽!
j007ha
2010-9-8 10:59 PM
回复 1# karen_er30


    total = 100 / dt.Rows[0]["total"];

先convert (dt.Rows[0]["total"])才算
goodday
2010-9-9 01:41 AM
unboxing 的问题 楼上的回答了
呵呵
karen_er30
2010-9-10 01:38 AM
本帖最后由 karen_er30 于 2010-9-10 01:40 AM 编辑

回复 2# j007ha




public partial class Inventory_ABCAnalysis : System.Web.UI.Page
{
   

    protected void Page_Load(object sender, EventArgs e)
    {



    }

    public string testing(int i)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connString"]);
        string sql = "SELECT COUNT[sellingcost] AS total FROM [Item]";

        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        cmd.CommandType = CommandType.Text;

        da.Fill(ds, "Item");
        dt = ds.Tables["Item"];
      
       string category;
      
        int total;
        total = 100 / Convert.ToInt32(dt.Rows[0]["total"]);

        if (total * i <= 20)
            category = "A";
        else if (total * i <= 50)
            category = "B";
        else if (total * i <= 100)
            category = "C";
        return category;

我改了之后,又出现以下问题。
Error: Use of unassigned local variable
是return category出了问题。。
宅男-兜着走
2010-9-13 12:30 PM
教你~这个是小问题。 你放个 Breakpoint 在你的 String Category 那句。 然后 一直next line next line 看你的程式怎么跑的, 你就懂跑的当儿少了什么了。
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2025-5-1 05:33 PM , Processed in 0.091823 second(s), 25 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部