我如何解决这个错误“system.invalidcastexception无法将'system.int64'类型的对象强制转换为'system.string'" [英] How do i solve this error "system.invalidcastexception unable to cast object of type 'system.int64' to type 'system.string' "
问题描述
protected string gridValueChopping(object dataItem, string dataField, int iSize)
{
string gridValue = (string)DataBinder.Eval(dataItem, dataField);
string output = string.Empty;
if (gridValue.Length > iSize)
{
output = ((string)DataBinder.Eval(dataItem, dataField)).Substring(0, iSize - 3);
output = output + "...";
}
else
{
output = (string)DataBinder.Eval(dataItem, dataField).ToString();
}
return output;
}
我得到了系统.invalidcastexception无法将'system.int64'类型的对象强制转换为'system.string'在上面提到的代码的以下行中出错 -
string gridValue =(字符串)DataBinder.Eval(dataItem,dataField);
我可以为此获得解决方案吗?
我尝试了什么:
I am getting the "system.invalidcastexception unable to cast object of type 'system.int64' to type 'system.string'"error in the following line of the above mentioned code -
string gridValue = (string)DataBinder.Eval(dataItem, dataField);
can I get a solution for this?
What I have tried:
string gridValue = (string)DataBinder.Eval(dataItem, dataField);
我改变了这一行,
I changed this line as,
string gridValue = (string)DataBinder.Eval(dataItem, dataField).ToString;
但即使这样,错误也会重复..
but even then the error repeats..
推荐答案
另一种选择:
Another alternative:
string gridValue = DataBinder.Eval(dataItem, dataField, "{0}");
另外,请不要 Eval
多次相同的值;使用从第一次调用 Eval
中检索到的值:
Also, don't Eval
the same value multiple times; use the value retrieved from the first call to Eval
instead:
protected string gridValueChopping(object dataItem, string dataField, int iSize)
{
string gridValue = DataBinder.Eval(dataItem, dataField, "{0}");
string output;
if (gridValue.Length > iSize)
{
output = gridValue.Substring(0, iSize - 3);
output = output + "...";
}
else
{
output = gridValue;
}
return output;
}
long longValue = (long)DataBinder.Eval(dataItem, dataField);
string gridValue = longValue.ToString();
应该这样做,或者至少应该允许更好地了解问题所在。
should do it, or at least should allow to better understand where the problem lies.
这篇关于我如何解决这个错误“system.invalidcastexception无法将'system.int64'类型的对象强制转换为'system.string'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!