开发者

How To Return Null From The Method Where We Return Data Table C# Web Application

how to return the null or some null table from the method where i return data table in case of some error my code is given below,

  public DataTabl开发者_开发知识库e CostOfKilowat()
        {
            try
            {
                DataTable CostDT = new DataTable();
                int Dollar = Convert.ToInt32(txtCommission.Text);
                CostDT = GetProducts();
                DataTable Costtable = GetCostRate(CostDT, Dollar, "DATE,MTU,POWER,COST,VOLTAGE,PERUNITCOST", "DATE", "Group by ");
                return Costtable;
            }
            catch 
            {
                String script = "<script>alert('Enter Valid Cost')</script>";
                Page.RegisterStartupScript("script", script);
            }
            return ;
        }

hopes for your suggestions...


Change the last line to:

return null;


From your function definition, I'm assuming you want to return a table so that you can bind that information to a grid or something?

Are you asking how to return an empty grid, or did you really want to know how to return null?

For the empty grid, with all the column names brought back, you would just need to make sure the stored procedure (or whatever server call you make), still selects all the fields even when the where clause makes it return nothing.

Then you have to change your code to always return the table:

public DataTable CostOfKilowat()
{
    DataTable CostDT = new DataTable();
    try
    {
        int Dollar = Convert.ToInt32(txtCommission.Text);
        CostDT = GetProducts();
        DataTable Costtable = GetCostRate(CostDT, Dollar, "DATE,MTU,POWER,COST,VOLTAGE,PERUNITCOST", "DATE", "Group by ");
        return Costtable;
    }
    catch 
    {
        String script = "<script>alert('Enter Valid Cost')</script>";
        Page.RegisterStartupScript("script", script);
    }
    return CostDT;
}

If you really want to return null... um.. return null;


added another easy answer according to answer in the comments of question :

  public DataTable CostOfKilowat()
        {
            try
            {
                DataTable CostDT = new DataTable();
                int Dollar = Convert.ToInt32(txtCommission.Text);
                CostDT = GetProducts();
                DataTable Costtable = GetCostRate(CostDT, Dollar, "DATE,MTU,POWER,COST,VOLTAGE,PERUNITCOST", "DATE", "Group by ");
                return Costtable;
            }
            catch(Exception ex)
            {
                String script = "<script>alert('" + ex.Message + "')</script>";
                Page.RegisterStartupScript("script", script);
                return null;
            }
        }

Not sure if I get it, but maybe that's what you want to get.


Hi his is how I'd write it:

public DataTable CostOfKilowat()
    {
        DataTable Costtable = null;
        try
        {
            int Dollar = Convert.ToInt32(txtCommission.Text);
            DataTable CostDT = GetProducts();
            Costtable = GetCostRate(CostDT, Dollar, "DATE,MTU,POWER,COST,VOLTAGE,PERUNITCOST", "DATE", "Group by ");
        }
        catch 
        {
            String script = "<script>alert('Enter Valid Cost')</script>";
            Page.RegisterStartupScript("script", script);
        }
        return Costtable;
    }


You can do it this way.

return new DataTable();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜