The following chock of C# code will throw an error, if the column name does not exist in the DataTable.

//Error Code:
string strFieldName = "****";
if (ds != null &&
 ds.Tables[0] != null &&
 ds.Tables[0].Rows.Count > 0 &&
 ds.Tables[0].Rows[0][strFieldName] != null)
{
 string strValue = ds.Tables[0].Rows[0][strFieldName].ToString().Trim();
}

//Correct Code:
string strFieldName = "****";
if (ds != null &&
 ds.Tables[0] != null &&
 ds.Tables[0].Rows.Count > 0 &&
 ds.Tables[0].Columns.Contains(strFieldName) == true &&
 ds.Tables[0].Rows[0][strFieldName] != null)
{
 string strValue = ds.Tables[0].Rows[0][strFieldName].ToString().Trim();
}
posted on 2006-12-14 14:05  袁礼定  阅读(316)  评论(0编辑  收藏  举报