如何从我返回数据表的方法返回null或某些null表,如果出现一些错误,我的代码在下面给出,
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
{
String script = "<script>alert('Enter Valid Cost')</script>";
Page.RegisterStartupScript("script", script);
}
return ;
}
希望你的建议......
答案 0 :(得分:3)
将最后一行更改为:
return null;
答案 1 :(得分:1)
从您的函数定义中,我假设您要返回一个表,以便将该信息绑定到网格或其他内容?
您是在询问如何返回空格,还是您真的想知道如何返回null?
对于空网格,带回所有列名称,您只需要确保存储过程(或您所做的任何服务器调用),即使在{{1}时仍然selects
所有字段}}子句使它不返回任何内容。
然后你必须改变你的代码总是返回表:
where
如果你真的想要返回null ...嗯.. 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;
}
答案 2 :(得分:0)
根据问题评论中的回答添加了另一个简单的答案:
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;
}
}
不确定我是否得到它,但也许这就是你想得到的。
答案 3 :(得分:0)
嗨,他是我写的:
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;
}
答案 4 :(得分:0)
您可以这样做。
返回新的DataTable();