JBTALKS.CC

标题: 我的asp.net c# coding有问题,帮帮忙! [打印本页]

作者: karen_er30    时间: 2010-9-8 06:16 PM
标题: 我的asp.net c# coding有问题,帮帮忙!
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 (https://www.jbtalks.cc/) Powered by Discuz! X2.5