我现在有一个DataTable,代码如下:
DataTable dt = dcFunction.GetDataTable(funcId);
当我在遍历dt时:
for(int i=0;i<dt.Rows.Count;i++)
{
string str1 = dt.Rows[i]["authorName"];
string str2 = dt.Rows[i]["funcName"];
}
但是如果这个返回的dt为空的时候,也就是没有查询到数据时,总是报错,说dt中没有数据,可是我想要得到的是如果dt中没有数据的话,把str1和str2的赋为"",而不是报错,我想屏蔽掉这个错误!!也就是说查到数据时str1和str2中就是查到的数据,如果查不到,str1和str2 等于""
DataTable dt = dcFunction.GetDataTable(funcId);
当我在遍历dt时:
for(int i=0;i<dt.Rows.Count;i++)
{
string str1 = dt.Rows[i]["authorName"];
string str2 = dt.Rows[i]["funcName"];
}
但是如果这个返回的dt为空的时候,也就是没有查询到数据时,总是报错,说dt中没有数据,可是我想要得到的是如果dt中没有数据的话,把str1和str2的赋为"",而不是报错,我想屏蔽掉这个错误!!也就是说查到数据时str1和str2中就是查到的数据,如果查不到,str1和str2 等于""
解决方案 »
- 关于web开发
- using的用法问题
- GridView首列復選框被選中改變顔色,不知道怎麽做?
- 为什么Gridview的SelectedIndexChanged要执行两次才能看到效果?
- 问个奇怪的问题,关于globalization的
- 怎样动态改变frame框架的宽度??
- 请问如何写一个通用程序或dll,记录窗口输入,比如user?
- 版主为什么这几天登录csdn没加可用分?
- 初学者问题:在*.aspx.cs 中这几行起什么作用?功能是什么?请各位大哥详细讲一讲。
- (散分!!!!~~~~散分!!!!~~~~))大家来讨论下用DREAMWEAVER MX用来做ASP.NET是否比Microsoft Visual Studio .NET更好更方便
- PetShop 3.0 下的Install.cmd,干什么用的?
- 100分送上,如何在WEB页面点击提交后提交到服务器上,请高手详细指教小妹
string str2 = String.Empty;
然后
for(int i=0;i<dt.Rows.Count;i++)
{
str1 = dt.Rows[i]["authorName"];
str2 = dt.Rows[i]["funcName"];
}
{
string str1 = dt.Rows[i]["authorName"];
string str2 = dt.Rows[i]["funcName"];
}变量重定义了。另外,没有数据应该不会报错,比如如果没数据,下面这个就应该为空字符串。dt.Rows[i]["authorName"].ToString();
string str2 = "";for(int i=0;i<dt.Rows.Count;i++)
{
str1 = dt.Rows[i]["authorName"].ToString();
str2 = dt.Rows[i]["funcName"].ToString();
}
string str2 = (dt.Rows[i]["funcName"] == null) ? String.Empty : (string)dt.Rows[i]["funcName"];
Or
str1 = dt.Rows[i]["authorName"].ToString()+"";